我按照以下方式创建了一个 AlarmManager。在特定时间后,服务使用 stopSelf() 停止,但警报管理器再次启动服务。如何停止服务内的 AlarmManager?活动不再运行,我必须在服务内部进行。
使用 AlarmManager 定期启动服务的活动:
Intent i=new Intent(this, AppService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if((tb_runBG.isChecked()))
{
alarmManager.cancel(pi);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 *update_time, pi);
}
else
{
// From Activity it's no problem to stop the AlarmManager
alarmManager.cancel(pi);
stopService(new Intent(this, AppService.class));
}
我尝试停止 AlarmManager 的服务:
Intent i=new Intent(this, volumeActivity.class);
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
// Her I should get the getSystemService from the Activity, but how??
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Following unfortunately not working ...
alarmManager.cancel(pi);
stopSelf();
不幸的是,它不起作用。Servive 停止,但 AlarmManager 再次调用它...
编辑:
以下也不起作用:
AlarmManager alarmManager = (AlarmManager) this.getBaseContext().getSystemService(Context.ALARM_SERVICE);
或者
AlarmManager alarmManager = (AlarmManager) this.getApplicationContext().getSystemService(Context.ALARM_SERVICE);