Android 在报警时间后使用报警管理器调用的停止服务
我需要帮助在alarm Manager中指定的时间过期后自动停止服务。代码如下:Android 在报警时间后使用报警管理器调用的停止服务,android,android-service,alarmmanager,Android,Android Service,Alarmmanager,我需要帮助在alarm Manager中指定的时间过期后自动停止服务。代码如下: Intent c = new Intent(getApplicationContext(), CallService.class); PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, c, 0); AlarmManager alarm = (Alar
Intent c = new Intent(getApplicationContext(), CallService.class);
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, c, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, 9*10000, pintent);
即使在指定的时间到期后,服务仍在后台运行,我如何停止服务以在时间到期后立即停止?我使用Timertask在指定时间后取消此意图:
Timer t = new Timer();
TimerTask welcome = new TimerTask(){
@Override
public void run() {
//Stopped the service
}
};
t.schedule(welcome, 300, 30000);
下面的网页详细介绍了如何使用timertask
呼叫报警。取消(pintent);
在指定的睡眠时间后尝试此操作,或者将其写在alarm.set()下面;
很可能只有在时间到期后才会调用它。如果这不能达到目的,请在指定的时间内使用thread.sleep,然后调用
报警取消(pintent) 我最初的想法是在服务开始倒计时时使用Timertask,并在正确的时刻执行stopself-on服务。这是一种正确的方法还是非常耗费资源?