android应用程序的一个常见用例是在手机启动时启动它们,并定期执行一些代码。
听起来很简单,但是有一些陷阱,因此,为了达到此目的,需要采取几个步骤。 从琐碎的东西开始:
在清单中,您需要执行以下操作才能接收启动事件:
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
然后,再次在清单中,您需要两个接收器:
<receiver android:name=".StartupLauncher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".Notifier">
<intent-filter>
<action android:name="com.yourpackage.HOURLY_CHECK" />
</intent-filter>
</receiver>
这些都是什么 您将在一分钟内看到HOURLY_CHECK。 我从多个StackOverflow 答案中收集的.StartupLauncher
的条目,声称可以在多个设备上工作。 通常您需要消耗的只是BOOT_COMPLETED
,其余的则以防万一。 请注意接收器的enabled
属性-通常不需要它们,但请确保父应用程序未禁用该接收器。 老实说,我不建议“以防万一”放置无用的东西,但是鉴于android设备的怪癖,我会保留它们。
现在实现将接收BOOT_COMPLETED
事件的服务:
public class StartupLauncher extends BroadcastReceiver {
@Override
public void onReceive(final Context ctx, Intent intent) {
AlarmManager alarmManager = (AlarmManager) ctx.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("com.yourpackage.HOURLY_CHECK");
PendingIntent notificationIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 1, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, TimeUnit.MILLISECONDS.convert(60, TimeUnit.MINUTES), notificationIntent);
}
}
为什么不使用ScheduledExecutorService呢? 因为一旦调度了Runnable
,您的BroadcastReceiver
就会死亡,而执行者也会随之死亡,因此永远不会调用该runnable。
现在,您有了预定的警报,您可以在其中执行实际的逻辑:
public class Notifier extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
// ...
}
}
您当然可以有一个BroadcastReceiver
并根据意图进行区分,但是我认为这更干净。
总的来说,这是一个棘手的过程–不幸的是,这通常发生在Android上。
翻译自: https://www.javacodegeeks.com/2014/04/scheduling-repeated-tasks-in-android.html