应用程序被 Android 或任务杀手杀死后如何保存预定的闹钟?
安排警报的代码。
PendingIntent sender = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, time, sender);
它工作正常,但是当我在任务杀手中杀死我的应用程序时,我丢失了预定的警报。如何解决这个问题呢?
Code that schedules alarm.
PendingIntent sender = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, time, sender);
Its working fine, but when I kill my app in task killer, I lost my scheduleed alarm. How to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让您的应用程序在被杀死时广播一条消息,当广播此消息时,让侦听器检查该服务是否仍在运行。如果没有运行它。这将确保即使应用程序被终止,您的服务仍在运行。
更新
我会尝试为您创建流程图
">
我希望这有帮助。
更新 2
我忘记提及的一件事是,理想情况下您只希望运行该服务的一个实例。因此,只需查看 onStart() 中存在的 ID 就应该 == 为 1 才能启动它,否则.. 忽略它。
Service 类的通知方法:
onStart() :服务启动时调用该方法
onDestroy() :服务被杀死时调用该
方法BroadcastReciever 类:
onReceive():此方法接收发送给它的所有意图(除非经过过滤)
查找有关 BroadcastRecievers(消息广播)和 Service(启动服务)的示例
参考:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
http://developer.android.com/reference/android/app/Service。 html
have your application broadcast a message as its being killed, and when this message is broadcast, then have a listener check if the service is still running.. if its not run it. This will insure that your service is running even if the application is killed.
Update
I'll try to create a flow diagram for you
The onDestroy() method is part of a service.
I hope this helps.
UPDATE 2
One thing I forgot to mention is the fact that you ideally only want one instance of the service to be run. So just looking at the ID that is present within the onStart() should be == to 1 to start it else.. ignore it.
Methods of notice of the Service Class:
onStart() : This method is called when the service is being started
onDestroy() : This is the method that is called when a service is being killed
Methods of notice of the BroadcastReciever class:
onReceive(): This methods receives all intents that are sent to it (unless filtered)
Look up examples on BroadcastRecievers (Message Broadcasting) and Service (Starting a service)
References:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
http://developer.android.com/reference/android/app/Service.html
当应用程序关闭时,警报管理器设置的警报不会被终止,但是当重新启动时,所有警报都会被操作系统清除,因为没有持久性。所以你需要坚持下去。
重新启动时再次设置闹钟。
Manifest.xml
您可以使用 SharedPreference 来保存时间(应触发警报的时间或下次应触发警报的时间),
用它在启动接收器上设置新的警报。
Alarm set by alarm manager is not killed when app is closed, how ever when a reboot occurs all alarms are cleared by the os since there is no persistence. So you need to do the persistence.
Set the alarm again on reboot.
Manifest.xml
You could use SharedPreference to save the time (time at when the alarm should be triggered or time at when it should be triggered next)
Use that to set a new alarm at the boot receiver.