如何设置在Android的持久性/定期? [英] How to set a persistent/regular schedule in Android?

查看:191
本文介绍了如何设置在Android的持久性/定期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能每次规定的时间(例如在凌晨5点每天)上执行动作(也许一个Intent)?它留设备重启后,类似的cron是如何工作的。

How can I execute an action (maybe an Intent) on every specified time (e.g. Every day on 5AM)? It has to stay after device reboots, similar to how cron works.

我不知道我是否可以使用 AlarmManager 这一点,也可以吧?

I am not sure if I can use AlarmManager for this, or can I?

推荐答案

如果你想让它留在设备重新启动后,你有设备重启后调度报警。

If you want it to stay after the device reboots, you have to schedule the alarm after the device reboots.

您需要有<一个href="http://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED">RECEIVE_BOOT_COMPLETED在你的Andr​​oidManifest.xml许可

You will need to have the RECEIVE_BOOT_COMPLETED permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

一个BroadcastReceiver需要以及拍摄意图<一href="http://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED">ACTION_BOOT_COMPLETED

<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

最后,将覆盖在你的BroadcastReceiver中的onReceive方法。

Lastly, override the onReceive method in your BroadcastReceiver.

public class BootcompletedReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
     //set alarm
  }
}

编辑:看<一href="http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29">setRepeating AlarmManager的方法来安排的Andr​​oid的cron'。

Look at the setRepeating method of AlarmManager to schedule the 'Android cron'.

这篇关于如何设置在Android的持久性/定期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆