Alarm standby alarm does not cancel

I read a lot of questions and answers on Stackoverflow, and many of them simply emphasize a .cancel()unique unique identifier. However, now it’s important how many times I tried, I just can’t cancel it.


My unique identifier

final static int RQS_1 = 1337;


My function is setAlarm. pickTime is the current activity, and timesUp is another class Servicethat shows a toast when time increases.

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

    }

}
+5
source share
3 answers

Well, to cancel the Alarm you have to create the same PendingIntent that you created at startup. You start,

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

You are canceling

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

Are they the same?

, . PendingIntent Service, PendingIntent BroadCast.

+2

, - (4-) PendingIntent.getService(pickTime.this, RQS_1, intent, 0); .

PendingIntent.FLAG_CANCEL_CURRENT 0, .

+1

, ,

@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**

OnStart . onStartCommand().

. :

Android

- Audio Control, .

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

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

, getBroadcast() pendingIntents. (, onStartCommand():)) .

0

All Articles