2013-08-30 79 views
0

我正在使用廣播接收器讀取Intent.ACTION_TIME_TICK並使其每分鐘播放一次服務音頻。一切正常,除非屏幕關閉,它不再發出聲音。如果我保持屏幕打開或者在屏幕關閉的情況下插入USB電纜,它將重新打開。請幫忙,謝謝!Android廣播接收器在屏幕關閉後不會觸發

PS。經過幾次測試,我發現這隻發生在我的android 2.3.6手機上,另一方面在我的4.1.2上,它運行沒有問題。

@Override 
public IBinder onBind(Intent arg0) { 
    return null; 
} 

@Override 
public void onCreate() { 
    mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    registerReceiver(new TickReceiver(), new IntentFilter(
      Intent.ACTION_TIME_TICK)); 
    mp = MediaPlayer.create(this, R.raw.cuckoo); 

} 

@Override 
public void onStart(Intent intent, int startId) { 

    notifyMe(); 

} 

@Override 
public void onDestroy() { 


    clearNotification(); 

    if (mp != null){ 
     mp.release(); 
     mp = null; 
    } 

} 

public void notifyMe() { 
    note = new Notification(R.drawable.front, "Message Inbound!", 
      System.currentTimeMillis()); 

    PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, 
      LeftAndRightActivity.class), Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_NEW_TASK); 
    note.setLatestEventInfo(this, "Title...", "Subtitle", i); 

    note.flags = note.flags | Notification.FLAG_ONGOING_EVENT; 

    mgr.notify(NOTIFY_ME_ID, note); 
} 

public void clearNotification() { 
    mgr.cancel(NOTIFY_ME_ID); 
} 

public void playBeep() { 

    mp.start(); 

} 

public class TickReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     playBeep(); 

    } 
} 
+0

什麼是最小的SDK你一直保持着明顯的? –

+0

min sdk是10 –

回答

0

嘗試在OnCreate中這樣做:

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    BroadcastReceiver mReceiver = new ScreenReceiver(); 
    registerReceiver(mReceiver, filter);