问题描述
我怎么能每次规定的时间(例如在凌晨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.
您需要有RECEIVE_BOOT_COMPLETED在你的AndroidManifest.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需要以及拍摄意图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
}
}
编辑:看setRepeating AlarmManager的方法来安排的Android的cron'。
Look at the setRepeating method of AlarmManager to schedule the 'Android cron'.
这篇关于如何设置在Android的持久性/定期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!