我有一项服务(在 Manifest 文件中正确声明),它会onDestroy()
安排自己在一分钟后通过AlarmManager
. 但是,即使onDestroy()
运行正确,该服务也没有启动。有什么问题?
调度代码:
@Override
public void onDestroy() {
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
c.startService(new Intent(c, MyService.class));
}
};
registerReceiver(br, new IntentFilter("xxxx"));
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent("xxxx"), 0);
AlarmManager am = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + ONE_MINUTE, pi);
Log.i("onDestroy", "Service scheduled.");
unregisterReceiver(br);
super.onDestroy();
}
Manifest 文件中的服务声明:
<service
android:name="com.xxxx.MyService"
android:exported="true">
</service>