我想在 AlarmManager
的帮助下显示每日通知。
它一直工作正常,直到我尝试使其在重新启动后保持不变
我按如下方式创建警报:
if (switch_notif.isChecked()){ //in my activity's code
NotificationReceiver.scheduleAlarms(getApplicationContext()); //allow the notification
} else {
NotificationReceiver.stopNotif(getApplicationContext()); //disable them
}
这是我的广播接收器:
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();
}
}
编辑: 这是我关闭和打开闹钟时的日志:
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
最佳答案
也许,alarmUp
始终为 false,因为您使用不同的Intent
来比较它们
检查它们是否存在:
boolean alarmUp = (PendingIntent.getBroadcast(context, 100, new Intent(), PendingIntent.FLAG_NO_CREATE) != null);
创建闹钟:
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);
正如您所看到的,两个语句中的 Intent
使用是不同的。
尝试检查如下:
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);
顺便说一句
所有警报都会在重新启动期间被销毁...因此,您应该注册 BroadcastReceiver
以接收 BOOT_COMPLETED
事件。这样,当您收到 BOOT_COMPLETED
事件时,您就知道您的设备已重新启动并且没有警报处于 Activity 状态...然后,您可以再次创建它们。
关于android - 检查 AlarmManager 是否存在总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481423/