0

所以我有一段代码安排警报如下

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

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

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

4

0 回答 0