应用程序被 Android 或任务杀手杀死后如何保存预定的闹钟?

发布于 2024-11-05 06:48:36 字数 325 浏览 3 评论 0原文

安排警报的代码。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2024-11-12 06:48:37

让您的应用程序在被杀死时广播一条消息,当广播此消息时,让侦听器检查该服务是否仍在运行。如果没有运行它。这将确保即使应用程序被终止,您的服务仍在运行。

更新

我会尝试为您创建流程图

">

我希望这有帮助。

更新 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

Death/Restart of a service

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

过去的过去 2024-11-12 06:48:37

当应用程序关闭时,警报管理器设置的警报不会被终止,但是当重新启动时,所有警报都会被操作系统清除,因为没有持久性。所以你需要坚持下去。

  • 每次设置闹钟时都会保存闹钟时间。
  • 注册一个接收器以完成启动。
  • 重新启动时再次设置闹钟。

    公共类 BootReceiver 扩展了 BroadcastReceiver {
    
        @覆盖
        公共无效onReceive(上下文上下文,意图意图){
            //重新注册闹钟
       }
    }
    

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
.......
<receiver
      android:name="BootReceiver"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

您可以使用 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.

  • Every Time while setting a alarm save the alarm time.
  • Register a receiver for boot completion.
  • Set the alarm again on reboot.

    public class BootReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //re register the alarm
       }
    }
    

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
.......
<receiver
      android:name="BootReceiver"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文