0

我有一个小问题。我使用AlarmManager在特定时间设置了我的通知。我为通知设置的时间存储在SQLLite数据库中。除了我重启手机的那一刻,他们都工作得很好。 alarmManager当然会放弃他们的重复。重新启动后设置闹钟电话

我想问一下这种情况下最好的解决方案是什么?我有我的alarmManager在MainActivity设置和设置我的里面广播接收器的通知,你可以在下面的代码中看到:

这是我如何把它从MainActivity:

 Intent intent = new Intent(context, MyReceiver.class); 
     intent.putExtra(EXTRA_TITLE, title); 
     intent.putExtra(EXTRA_COUNT, count); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, count, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), WEEK_LENGTH_MS, pendingIntent); 

这里是广播接收器的方法的onReceive

public void onReceive(Context context, Intent intent) 
    { 
     nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     CharSequence from = context.getString(R.string.app_name); 
     CharSequence message = intent.getStringExtra(DayActivity.EXTRA_TITLE); 
     Intent intentNotification = new Intent(context,DayActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, intent.getIntExtra(DayActivity.EXTRA_COUNT,0), intentNotification, 0); 
     Notification notif = new Notification(R.drawable.notification_logo,context.getString(R.string.app_name), System.currentTimeMillis()); 
     notif.setLatestEventInfo(context, from, message, contentIntent); 
     notif.defaults |= Notification.DEFAULT_LIGHTS; 
     notif.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
     nm.notify(intent.getIntExtra(DayActivity.EXTRA_COUNT,0), notif); 

    } 

我声明广播接收器的BOOT_COMPLETED事件,但它总是来电我启动手机,从来没有更多的时候只是空的通知。

回答

1

我想问一下在这种情况下最好的解决方案是什么?

注册一个BOOT_COMPLETEDBroadcastReceiver调用setRepeating()AlarmManager重新建立你的时间表。

我宣布为BOOT_COMPLETED事件的BroadcastReceiver,但它始终只是在我启动手机时调用空通知,而不会更多。

BOOT_COMPLETEDBroadcastReceiver的目标应该是重新安排您的警报。您可能希望考虑使用单独的BroadcastReceiver,而不是您正在使用的报警事件本身。

+0

所以你说要使用像OnBootReceiver的东西,我会得到时间,并使用alarmManager调用第一个BroadcastReceiver? – 2014-10-18 13:57:55

+1

@JanOmacka:是的。欢迎您有一个'BroadcastReceiver'处理这两个角色,但您需要区分引导事件和警报事件,例如通过检查传入的“Intent”的操作字符串。例如,[这个示例应用程序](https://github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/WakeCast)使用该方法,因为BOOT_COMPLETED广播将具有非'null'动作字符串,而我在'PendingIntent'中用'AlarmManager'使用的显式'Intent'将有一个'null'动作字符串。 – CommonsWare 2014-10-18 14:02:37

+0

非常感谢您的帮助。 – 2014-10-18 14:10:20