我有一个活动,从警报广播接收器调用,它每 N 秒播放一次通知声音。这是通过以下代码实现的:
repeating_notification_sound_timer.schedule(ring_the_buzzer_task, 0, 1000 * N);
在活动的 onCreate 函数的早期,我有以下代码:
pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
只要手机在触发警报时处于唤醒状态,这一切都运行良好。但是,如果手机处于睡眠状态,则通知声音只会播放一次,并且不会再播放一次。该程序不会崩溃或报告任何错误。好像 repeating_notification_sound_timer 只能工作一次!有任何想法吗?
编辑:这是首先设置警报的代码:
static void set_alarm(long alarm_time_in_millis,Context cont,AlarmManager alarm_manager,String str)
{
Intent launchIntent = new Intent(cont, to_call_when_alarm_goes_off.class);
launchIntent.putExtra("string_passed_in_bundle", str);
launchIntent.setAction(to_call_when_alarm_goes_off.CUSTOM_INTENT);
PendingIntent pIntent = PendingIntent.getBroadcast(cont,0, launchIntent, 0);
alarm_manager.cancel(pIntent);
alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent);
}
编辑:在清单中我有:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />