我正在使用AlarmManager。我和Handler,TImerTasks一起走了一圈,意识到他们都不能在后台工作。现在这是我的目标:
这是我现在正在使用的代码:
private Handler handler = new Handler();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
final Runnable runnable = new Runnable() {
@Override
public void run() {
Intent sendMessage = new Intent();
sendMessage.setAction(laway);
sendMessage.setClass(lawayService.this, alarmReceiver.class);
sendBroadcast(sendMessage);
}
};
handler.postDelayed(runnable, DURATION);
return START_STICKY;
}