我正在使用触发服务的WakefulBroadcastReceiver来唤醒我的设备。该服务启动一个Activity,最后通知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);
答案 0 :(得分:0)
我建议您使用服务并在那里进行处理。我认为很可能你的应用程序被杀死了,因此链接到创建它的Activity的IntentService也被杀死了。 尝试使用完整的服务范围,它必须解决您的问题。