我有一个 android setInexactRepeating 放在我的 onCreate 里面,它永远不会触发。我在其中有一个日志,以确保它实际上正在执行,并且似乎没有触发,以及我为它计划的事件。我希望它每 10 秒熄灭一次,但即使是第一次,它似乎也不会熄灭。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Restart", "First");
Intent toRun = new Intent(this, AlarmRestart.class);
PendingIntent pendingToRun = PendingIntent.getBroadcast(this, 0, toRun, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pendingToRun);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 10000L, pendingToRun);
Log.d("Restart", "Second");
}
这是在另一个文件中:
public class AlarmRestart extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("Restart", "Third");
}
}
这就是“adb shell dumpsys 警报”所说的
com.packageName.restart
5715ms running, 64 wakeups
3 alarms: flg=0x14 cmp=com.packageName.restart/.AlarmRestart
61 alarms: flg=0x14 cmp=com.packageName.restart/.reciever.AlarmRestart