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

さて、私の問題は、ユーザーがアラームを削除し、その後コードのスケジュールされていない部分を呼び出すと、volumeServiceすぐに呼び出されて設定が変更されることです。ただし、これはユーザーがアラームを削除する目的を無効にします。ユーザーがアラームを削除するのは、アラームが起動して設定が変更されるのを防ぐためだけだからです。私は何週間にもわたって考えられるあらゆる場所をチェックしてきましたが、頭を壁にぶつけています。ここに投稿したので、非常に単純な間違いを犯したことになります。いずれにせよ、事前に助けてくれてありがとう!

--編集 #1-- unscheduler 部分の uPintent.cancel() 行を削除しましたが、まだ機能しません。

4

0 に答える 0