- Tools
- dumpsys
Check if scheduled task is registered with:
$ adb shell dumpsys alarm
- xxx
- AlarmMamager
- set/setRepeating
Register a scheduled task. 4 types of alarm
- ELAPSED_REALTIME: Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
- ELAPSED_REALTIME_WAKEUP: Like ELAPSED_REALTIME bue will wake up the device when it goes off.
- RTC: Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
- RTC_WAKEUP: Like RTC will wake up the device when it goes off.
Notes: If alarm type is XXX_WAKEUP, and device is in sleeping mode, the alarm may be triggered several seconds ~ several minutes after the specified triggerring time.
- xxx
- If timer is set to the past, the timer will be triggerd immediately. But it only be triggered once, no matter how much times it is.
- If set system time foreward and exceed triggerring time, the timer will be triggering, and it is triggered only once, no matter how much it is.
- Change system time backward won't trigger timer.
- Trigger alarm and complete the specified task even if the device is in sleeping mode.
- Register alarm
...
PendingIndent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarm.set(AlarmManager.RTC_WAKEUP, time, sender);
...
Notes: you can replace PendingIntent.getBroadcast with PendingIntent.getService or PendingIntent.getActivity
- Lock power manager in Broadcast receiver/Service/Activity
In the initiating interface (onReceive/onCreate), lock poer manager, then executing tasks, when the task is completed, release the power manager (Pay attation, user still can make device into sleeping mode by pressing Power key).
- Lock power manager in onReceive/onCreate
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My lock name");
wl.setReferenceCounted(true);
wl.acquire();
- Release power manager in onReceive/onDestroy
if (wl != null)
{
wl.release();
wl = null;
}
- You can verify if power wake lock is acquired or released with command
$adb shell dumpsys power
then search the specified tag when you invoke pm.newWakeLock() - xxx
- Request permission in AndroidManifest.xml
- xxx
- xxx
- ..
阅读(2465) | 评论(0) | 转发(0) |