user4853635
user4853635

Reputation:

Alarm Manager and Pending Intent Cancel Not Working

I schedule Alarm from Activity like.

   private AlarmManager mAlarmManager;  
            mAlarmManager = (AlarmManager) ACT_ActiveSession.getAppContext()
                    .getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(mContext, LocalNotification.class);
            intent.putExtra("alertBody", "");
            intent.putExtra(K.SESSIONID, "");
            intent.putExtra("TIME", "");
            intent.putExtra("BATCHNO","");
            intent.putExtra("REQUEST_CODE", "");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    ACT_ActiveSession.getAppContext(), REQUEST_CODE, intent, 0);
            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, finishTime,
                    pendingIntent);
            // alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, finishTime,
            // (1 * 1000), pendingIntent);

intent.putExtra("",""); only used for some task on BroadcastReceiver

And Cancel Alarm From Fragment

private AlarmManager mAlarmManager; 

    mAlarmManager = (AlarmManager) mActivity
                            .getSystemService(Context.ALARM_SERVICE);
                    Intent updateServiceIntent = new Intent(mActivity,
                            ACT_Home.class);

                    PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(
                            mActivity, REQUEST_CODE,
                            updateServiceIntent, 0);
                     pendingUpdateIntent.cancel();
                    // Cancel alarms
                    if (pendingUpdateIntent != null) {
                        mAlarmManager.cancel(pendingUpdateIntent);
                        Log.e("", "alaram canceled ");
                    } else {
                        Log.e("", "pendingUpdateIntent is null");
                    }

But Alarm Manager is not cancelled. Here i change mActivity = MyActivity's static getApplicationContext(); and also change different Flags and different Context.

Also I refer many answer. But doesn't work any code. Link1 Link2 Link3

please give me solution as soon as possible. and apologize for my bad English.

Upvotes: 3

Views: 1126

Answers (1)

David Wasser
David Wasser

Reputation: 95636

You create the alarm using this Intent:

Intent intent = new Intent(mContext, LocalNotification.class);

But you try to cancel it using this Intent:

Intent updateServiceIntent = new Intent(mActivity, ACT_Home.class);

These Intents do not match, so the cancel does nothing. If you want to cancel the alarm you need to use an Intent in the cancel call that matches the Intent you used to schedule the alarm.

Upvotes: 1

Related Questions