在闹钟时间之后停止使用警报管理器调用的服务

时间:2013-10-27 17:01:04

标签: android android-service alarmmanager

我需要帮助才能在闹钟管理器中指定的时间过后自动停止服务。以下是代码:

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);

即使指定的时间过期后服务仍然在后台运行,如何在时间到期后立即停止服务停止?

2 个答案:

答案 0 :(得分:0)

我使用Timertask在指定时间后取消意图:

Timer t = new Timer();
    TimerTask welcome = new TimerTask(){

        @Override
        public void run() {
         //Stopped the service
        }
    };
    t.schedule(welcome, 300, 30000);

网页下方有详细介绍如何使用timertask http://www.codeproject.com/Questions/413950/TimerTask-in-Android

答案 1 :(得分:0)

call alarm.cancel(pintent); 在指定时间睡眠后尝试此操作或将其写在alarm.set()下面; 最有可能的是它只会在时间到期后被调用。如果没有达到目的,那么在指定的时间内使用thread.sleep然后调用 alarm.cancel(pintent);

相关问题