Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm Pending Alarm doesn't cancel

I've read through many questions and answers on Stackoverflow, and many of which just emphasize on the .cancel() and the special unique ID. However, now matter how many times I tried, I just can't cancel it.


My unique ID

final static int RQS_1 = 1337;


My setAlarm Function. pickTime is current Activity, and timesUp, is another Service class that shows a toast when the time is up.

Intent intent = new Intent(pickTime.this, timesUp.class);
PendingIntent timesUpIntent = PendingIntent.getService(pickTime.this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
                 timesUpIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), timesUpIntent);

My cancelAlarm function

Intent intent = new Intent(this, pickTime.class);
PendingIntent timesUpIntent = PendingIntent.getBroadcast(this, RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        if (timesUpIntent != null) {
            alarmManager.cancel(timesUpIntent);
            timesUpIntent.cancel();
            Toast.makeText(getApplicationContext(), "Alarm is cancelled",
                    Toast.LENGTH_SHORT).show();
                    } else {

            Toast.makeText(getApplicationContext(), "Unable to stop timer",
                    Toast.LENGTH_SHORT).show();
        }

My timesUp Service

public class timesUp extends Service {

    @Override
    public void onCreate() {

        // TODO Auto-generated method stub

        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
                .show();

        return null;

    }

    @Override
    public void onDestroy() {

        // TODO Auto-generated method stub

        super.onDestroy();

        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public void onStart(Intent intent, int startId) {

        // TODO Auto-generated method stub

        super.onStart(intent, startId);

        Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
                .show();

        return super.onUnbind(intent);

    }

}
like image 392
MrYanDao Avatar asked Oct 23 '22 12:10

MrYanDao


2 Answers

Well for cancelling Alarm you have to create the same PendingIntent that you created while starting it. You are doing while starting,

Intent intent = new Intent(pickTime.this, timesUp.class);
PendingIntent timesUpIntent = PendingIntent
                                .getService(pickTime.this, RQS_1, intent, 0);

You are doing while cancelling,

Intent intent = new Intent(this, pickTime.class);
PendingIntent timesUpIntent = PendingIntent
                                        .getBroadcast(this, RQS_1, intent, 0);

Are they same?

No, they are not same. You are creating PendingIntent for Service to start and trying to cancel with different PendingIntent of BroadCast.

like image 76
Lalit Poptani Avatar answered Oct 31 '22 09:10

Lalit Poptani


I suspect there is something wrong with the last(4th) argument of the method PendingIntent.getService(pickTime.this, RQS_1, intent, 0); in your code.

Try using PendingIntent.FLAG_CANCEL_CURRENT instead of 0, it should work.

like image 27
sujith Avatar answered Oct 31 '22 11:10

sujith