0

我正在使用广播接收器来读取 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();

    }
}
4

1 回答 1

0

尝试在您的 oncreate 中执行此操作:

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);
于 2013-08-30T09:27:25.350 回答