Skip to content Skip to sidebar Skip to footer

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 tri

Solution 1:

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

Intentintent=newIntent(pickTime.this, timesUp.class);
PendingIntenttimesUpIntent= PendingIntent
                                .getService(pickTime.this, RQS_1, intent, 0);

You are doing while cancelling,

Intentintent=newIntent(this, pickTime.class);
PendingIntenttimesUpIntent= 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.

Solution 2:

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.

Solution 3:

Not sure if this is the only problem but

@OverridepublicvoidonStart(Intent intent, int startId) {

    // TODO Auto-generated method stubsuper.onStart(intent, startId);

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

}**strong text**

The onStart is deprecated. For a service use onStartCommand().

See the example in the developer docs:

Android Services

I use something like this in my app Audio Control and it works well.

Intentintent=newIntent(getApplicationContext(), QuickReceiver.class);
intent.putExtra("extraKeyHere", "some extra if you like");

PendingIntentpi= PendingIntent.getBroadcast(getApplicationContext(),
    1137, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManageral= 
    (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);

al.setRepeating(AlarmManager.RTC_WAKEUP, endCal.getTimeInMillis(), 
    AlarmManager.INTERVAL_DAY, pi);

This is just creating an intent that will trigger a broadcast receiver (QuickReceiver.class).

publicclassQuickReceiverextendsBroadcastReceiver
{   
@OverridepublicvoidonReceive(Context context, Intent intent)
{   
        Bundleextras= intent.getExtras();
        if(extras != null)
        {   
        StringendProfile= extras.getString("extraKeyHere");

            Intenti=newIntent(context, QuickReceiver.class);

            PendingIntentpi= PendingIntent.getBroadcast(context,     
            1137, i, PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManageral=                                       
                 (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

            al.cancel(pi);
        }
    }
}

Make a note that I used "getBroadcast()" for both pendingIntents. You could start your service from the broadcast (using onStartCommand() of course :) ) and do more work there.

Post a Comment for "Alarm Pending Alarm Doesn't Cancel"