我正在使用广播接收器来读取Intent.ACTION_TIME_TICK并使其每分钟播放一次来自服务的音频。一切正常,除非屏幕关闭,它不再发出声音。如果我关闭屏幕或插入USB屏幕,屏幕关闭,它会重新开启。请帮忙,谢谢!
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 :(得分:0)
尝试在你的oncreate中执行此操作:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);