I am unable to retrieve the same intent even after passing the same ID of the intent.
I have checked the broadcast receiver has the exact same context used while creating the pending intent.
public class AlarmReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public static MediaPlayer mMediaPlayer;
public static NotificationManager notificationManager;
public static Notification notification;
public static int id;
Context ctx;
@Override
public void onReceive(final Context context, Intent intent) {
ctx = context;
Log.d("WTF", ""+intent.getAction());
if (intent.getAction() != null) {
String action = intent.getAction();
switch (action) {
case "SNOOZE":
Log.v("shuffTest", "Pressed Snoozed");
break;
case "STOP_ACTION":
Log.v("shuffTest", "Pressed Stop");
mMediaPlayer.stop();
notificationManager.cancel(id);
stopReceiverServices();
break;
}
} else {
notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
playAlarmSound();
}
// This is the Intent to deliver to our service.
//Intent service = new Intent(context, SimpleWakefulService.class);
// Start the service, keeping the device awake while it is launching.
//startWakefulService(context, service)
}
public void stopReceiverServices(){
Intent notificationIntent = new Intent(ctx.getApplicationContext(), AlarmReceiver.class);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
Log.d("TAG", "Context A1: "+ctx.getApplicationContext().toString());
Log.d("TAG", "Context A2: "+ctx.toString());
AlarmManager alarmManager = (AlarmManager) ctx.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Log.d("TAG", "AlarmManager Before: " + alarmManager);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("TAG", "PendingIntent Before: " + pendingIntent);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
Log.d("TAG", "AlarmManager After: " + alarmManager);
Log.d("TAG", "PendingIntent After: " + pendingIntent);
}
/..
}
While Creating the PendingIntent.
void setAlarm(Context context) {
mContext = context;
Intent notificationIntent = new Intent(context, AlarmReceiver.class);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, getNotification("Wake Up! Wake Up"));
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minuteOfHour);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.d("TAG", "AlarmManager After setting : " + alarmManager);
Log.d("TAG", "PendingIntent After setting : " + pendingIntent);
Log.d("Ctx setAlarm1 ", context.getApplicationContext().toString());
}
Logcat :
06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: AlarmManager After setting : android.app.AlarmManager@ed1e807
06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: PendingIntent After setting : PendingIntent{7c1d34: android.os.BinderProxy@ff6775d}
06-14 20:01:39.297 23767-23767/com.apps.testapp D/Ctx setAlarm1: com.apps.testapp.ApplicationMain@33d58d2
06-14 20:01:39.305 23767-23881/com.apps.testapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb91721a8
06-14 20:01:44.334 23767-23767/com.apps.testapp D/WTF: null
06-14 20:01:44.545 23767-23767/com.apps.testapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
06-14 20:01:49.772 23767-23767/com.apps.testapp D/WTF: STOP_ACTION
06-14 20:01:49.773 23767-23767/com.apps.testapp V/shuffTest: Pressed Stop
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A1: com.apps.testapp.ApplicationMain@33d58d2
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A2: android.app.ReceiverRestrictedContext@3af302a
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: AlarmManager Before: android.app.AlarmManager@ed1e807
06-14 20:01:49.780 23767-23767/com.apps.testapp D/TAG: PendingIntent Before: PendingIntent{cc07e1b: android.os.BinderProxy@8441bb8}
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: AlarmManager After: android.app.AlarmManager@ed1e807
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: PendingIntent After: PendingIntent{cc07e1b: android.os.BinderProxy@8441bb8}