'Check if AlarmManager exists always returning false
I want to show a daily notification with helping of AlarmManager
.
It was working properly until I tried to make it persistent after reboot
I create the Alarm as follows:
if (switch_notif.isChecked()){ //in my activity's code
NotificationReceiver.scheduleAlarms(getApplicationContext()); //allow the notification
} else {
NotificationReceiver.stopNotif(getApplicationContext()); //disable them
}
Here is my broadcast receiver:
public class NotificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//Log.d("NotificationReceiver", "onReceive");
this.sendNotif(context);
//trying to see if there is already an alarm in place (for reboot)
boolean alarmUp = (PendingIntent.getBroadcast(context, 100,
new Intent(),
PendingIntent.FLAG_NO_CREATE) != null);
Log.d("NotificationReceiver", alarmUp? "true":"false" ); //always return false
if (alarmUp)
{
//Log.d("NotificationReceiver", "Alarm is already active");
}
else { //if no alarm, schedule one
//Log.d("NotificationReceiver", "Alarm is not active");
this.scheduleAlarms(context);
}
}
public static void scheduleAlarms(Context context) {
//Log.d("NotificationReceiver", "scheduleAlarms");
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 13);
alarmStartTime.set(Calendar.MINUTE, 31);
alarmStartTime.set(Calendar.SECOND, 30);
/*if (now.after(alarmStartTime)) { // not needed when using 1min repeat for testing
Log.d("Alarm","Added a day");
alarmStartTime.add(Calendar.DATE, 1);
}*/
Intent intent = new Intent(context, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,alarmStartTime.getTimeInMillis(),60000 ,pendingIntent); // set to 1minute for testing purpose
}
public static void sendNotif(Context context){ //fire the notification
//Log.d("NotificationReceiver", "sendNotif");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context, Notification.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 100, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle("Rappel MyCardioPad")
.setContentText("Vous avez une séance d'éffort aujourd'hui")
.setSound(soundUri)
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
}
public static void stopNotif(Context context){ //Allow to turn off the alarm/notification
//Log.d("NotificationReceiver", "stopNotif");
Intent intent = new Intent(context, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
}
edit: This is the log from when I turn off and turn on the alarm:
07-20 14:22:58.637 mycardiopad D/NotificationReceiver: scheduleAlarms
07-20 14:22:59.856 mycardiopad D/NotificationReceiver: onReceive
07-20 14:22:59.856 mycardiopad D/NotificationReceiver: sendNotif
07-20 14:22:59.863 mycardiopad D/NotificationReceiver: false
07-20 14:22:59.863 mycardiopad D/NotificationReceiver: Alarm is not active
07-20 14:22:59.863 mycardiopad D/NotificationReceiver: scheduleAlarms
07-20 14:22:59.908 mycardiopad D/NotificationReceiver: onReceive
07-20 14:22:59.908 mycardiopad D/NotificationReceiver: sendNotif
07-20 14:22:59.912 mycardiopad D/NotificationReceiver: false
07-20 14:22:59.912 mycardiopad D/NotificationReceiver: Alarm is not active
07-20 14:22:59.912 mycardiopad D/NotificationReceiver: scheduleAlarms
07-20 14:23:00.509 mycardiopad D/NotificationReceiver: onReceive
07-20 14:23:00.509 mycardiopad D/NotificationReceiver: sendNotif
07-20 14:23:00.520 mycardiopad D/NotificationReceiver: false
07-20 14:23:00.520 mycardiopad D/NotificationReceiver: Alarm is not active
Solution 1:[1]
Maybe, alarmUp
is always false because your are using a different Intent
to compare them
Checking if they exist:
boolean alarmUp = (PendingIntent.getBroadcast(context, 100, new Intent(), PendingIntent.FLAG_NO_CREATE) != null);
Creating the alarm:
Intent intent1 = new Intent(context, Notification.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
As you can see, Intent
use in both statment are different.
Try to check as below:
Intent tempIntent = new Intent(context, Notification.class);
tempIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
boolean alarmUp = (PendingIntent.getBroadcast(context, 100, tempIntent, PendingIntent.FLAG_NO_CREATE) != null);
By the way
All alarms are destroyed during a reboot... So, you should register your BroadcastReceiver
to receive BOOT_COMPLETED
events. This way, when you receive BOOT_COMPLETED
event, you know that your device was rebooted and no alarm is active... Then, you crete them again.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | W0rmH0le |