Search code examples
androidalarmmanager

Cancel an AlarmManager pendingIntent in another pendingintent


I want cancel AlarmManager which define a service,in this service might start a new AlarmManger or cancel alarm that defined before.And I know the params pendingintent in alarmManager.cancel(PendingIntent),must be the same.compare with filterEquals(Intent other) but It still not work.cancel failed. here is my code

public class GetRoundStroe {
    private Store[] stores;
    private Context mContext;

    public GetRoundStroe(Context mContext) {
        this.mContext = mContext;
    }

    public Store[] getStores() {
        if (ComCommand.haveInternet(mContext)) {
            start_am_normal();
        } else {
            start_am_silence();
        }
        return stores;
    }

    public Store[] start_am_silence() {


        long firstTime = SystemClock.elapsedRealtime();

        AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

        if (AlarmHolder.mAlarmNormal != null) {
            am.cancel(AlarmHolder.mAlarmNormal);

        }

        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                firstTime, TestSwitch.getInstance().getSilence_time(), AlarmHolder.mAlarmSilence);


        return null;


    }

    public Store[] start_am_normal() {


        long firstTime = SystemClock.elapsedRealtime();

        AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

        if (AlarmHolder.mAlarmSilence != null) {
            MyLog.e(GetRoundStroe.class,"AlarmHolder.mAlarmSilence"+AlarmHolder.mAlarmSilence+"");
            am.cancel(AlarmHolder.mAlarmSilence);
        }
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                firstTime, TestSwitch.getInstance().getNormal_time(), AlarmHolder.mAlarmNormal);

        return null;
    }

    private static final class AlarmHolder {
        static final PendingIntent mAlarmSilence = PendingIntent.getService(ApplicationContext.getInstance(),
                0,
                new Intent(ApplicationContext.getInstance(), GetRoundSilenceService.class),
                0);

        static final PendingIntent mAlarmNormal = PendingIntent.getService(ApplicationContext.getInstance(),
                0, new
                Intent(ApplicationContext.getInstance(), GetRoundNormalService.class),
                0);

    }
}

GetRoundSilenceService and GerRoundNormalService invoke start_am_normal() or start_am_silence; Anyone could help me? thanks


Solution

  •    myIntent = new Intent(SetActivity.this, AlarmActivity.class);
       pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
           id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
       pendingIntent.cancel();
       alarmManager.cancel(pendingIntent);
    

    These lines of code surely can help you remove/cancel the pending intent and alarm.

    The main thing that you will need is:

    1. Create pending intent with the same id and appropriate intent FLAG.
    2. Cancel that pending intent.
    3. Cancel the alarm using alarm manager.