5

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

    }

}
sbridges
  • 24,960
  • 4
  • 64
  • 71
MrYanDao
  • 1,253
  • 1
  • 15
  • 27

3 Answers3

2

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

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.

sujith
  • 2,421
  • 2
  • 17
  • 26
0

Not sure if this is the only problem but

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

}**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.

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

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

AlarmManager al = 
    (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).

public class QuickReceiver extends BroadcastReceiver
{   
@Override
public void onReceive(Context context, Intent intent)
{   
        Bundle extras = intent.getExtras();
        if(extras != null)
        {   
        String endProfile = extras.getString("extraKeyHere");

            Intent i = new Intent(context, QuickReceiver.class);

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

            AlarmManager al =                                       
                 (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.

Matthew
  • 3,411
  • 2
  • 28
  • 28