我正在使用喚醒服務的WakefulBroadcastReceiver喚醒我的設備。該服務啓動一個活動,並在最後通知WakefulBroadcastReceiver結束作業。我做了一個日誌文件,我什麼都看不到。當我解鎖設備時,幾秒鐘後,活動即開始。當我設置鬧鐘時,爲什麼不做?爲什麼WakefulBroadcastReceiver不能將我的設備從深度睡眠中喚醒?
Manifest包含BroadcastReceiver和Service聲明和WAKE_LOCK權限。
這裏是WakefulBroadcastReceiver:
public class AlarmBroadcastReceiver extends WakefulBroadcastReceiver {
public AlarmBroadcastReceiver() {
Logging.logToFile("Construct broadcast receiver");
}
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AlarmService.class);
startWakefulService(context, service);
Logging.logToFile("Started wakeful broadcast receiver");
}
}
這裏是服務類:
public class AlarmService extends IntentService {
public AlarmService() {
super("AlarmService");
Logging.logToFile("Started intent service");
}
@Override
protected void onHandleIntent(Intent intent) {
Logging.logToFile("Handle intent service");
Intent service = new Intent(this, WakeUpActivity.class);
service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(service);
AlarmBroadcastReceiver.completeWakefulIntent(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Logging.logToFile("Destroy intent service");
}
}
這裏是我的日誌:
At 2016-04-01 23:14: Alarm set at 23:20
At 2016-04-01 23:23: Construct broadcast receiver
At 2016-04-01 23:23: Started wakeful broadcast receiver
At 2016-04-01 23:23: Started intent service
At 2016-04-01 23:23: Handle intent service
At 2016-04-01 23:23: Started WakeActivity
At 2016-04-01 23:23: Destroy intent service
At 2016-04-01 23:25: Alarm set at 23:30
At 2016-04-01 23:33: Construct broadcast receiver
At 2016-04-01 23:33: Started wakeful broadcast receiver
At 2016-04-01 23:33: Started intent service
At 2016-04-01 23:33: Handle intent service
At 2016-04-01 23:33: Destroy intent service
At 2016-04-01 23:33: Started WakeActivity
At 2016-04-01 23:34: Alarm set at 23:40
At 2016-04-01 23:42: Construct broadcast receiver
At 2016-04-01 23:42: Started wakeful broadcast receiver
At 2016-04-01 23:42: Started intent service
At 2016-04-01 23:42: Handle intent service
At 2016-04-01 23:42: Destroy intent service
At 2016-04-01 23:42: Started WakeActivity
At 2016-04-01 23:43: Started WakeActivity
At 2016-04-01 23:44: Alarm set at 23:49
At 2016-04-02 00:02: Construct broadcast receiver
At 2016-04-02 00:02: Started wakeful broadcast receiver
At 2016-04-02 00:02: Started intent service
At 2016-04-02 00:02: Handle intent service
At 2016-04-02 00:02: Started WakeActivity
At 2016-04-02 00:02: Destroy intent service
編輯:這是我如何設置鬧鐘:
Intent intent = new Intent(SmartClockActivity.this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(SmartClockActivity.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
請包括你如何設置報警 –
@DavidMedenjak,完成。 –