被取消的AlarmManager会立即调用待处理的意图

时间:2013-08-24 00:25:45

标签: android android-intent service alarmmanager android-pendingintent

所以我有一段代码可以按如下方式安排警报

public void scheduleAlarm(){
    Log.d("Scheduler","Alarm is being scheduled");
    Intent intent = new Intent(AlarmSettings.this, VolumeService.class);
    intent.putExtra("MODE", mode);
    PendingIntent pintent = PendingIntent.getService(AlarmSettings.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Log.d("Alarm ID:", String.valueOf(id));
    Log.d("Time", "Time was set for today: " + String.valueOf(time));
    if(time < System.currentTimeMillis()){
        time += (DAY);
        Log.d("Time", "Time was set for tomorrow: " + String.valueOf(time));
    }
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, time, pintent);
}

正在调用的服务如下

public class VolumeService extends Service{

@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //My Service code goes here and makes changes to some settings
    Log.d("Service", "settings have been changed");
    return START_NOT_STICKY;
 }


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

最后是应该取消闹钟的代码部分(留在我身边,我知道它很长)

//Unschedule the alarm that is getting deleted
    Log.d("Unscheduler", "Alarm is being unscheduled");
    Intent uIntent = new Intent(AlarmSettings.this, VolumeService.class);
    PendingIntent uPintent = PendingIntent.getService(AlarmSettings.this, id, uIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager uAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    uAlarm.cancel(uPintent);
    uPintent.cancel();

现在,我的问题是当用户删除警报并随后调用代码的非预定部分时,会立即调用volumeService并更改设置。但是,这会破坏用户删除警报的目的,因为他们只会删除它以防止它被触发和更改设置。在几周的时间里,我已经检查了所有可以想象到的地方,我只是在墙上敲打着我的头。既然我已经将它发布在这里,那将是我犯了一个非常简单的错误。无论如何,感谢您的帮助!

- 编辑#1-- 删除了uncheduler部分中的uPintent.cancel()行,但仍然无法正常工作。

0 个答案:

没有答案