Is there an easier way to use AlarmManager or set alarms in real time?

It seems that the only way for the action time based on the actual elapsed time (as opposed to the uptime that stops when the device is changed) is AlarmManager.

Is there an easy way to make a delayed exectuion based on a "wallclock", for example, through the open source shell around AlarmManager?

For normal synchronization operations, you can use a handler that is as simple as a simple task:

  • Implement a handler callback (without registering)
  • Create handler
  • Call sendEmptyMessageDelayed or similar functions
  • To clear all your delays just call removeCallbacksAndMessages(null)

However, Handler only supports time-dependent delays, which are sometimes insufficient (for example, if you want to check the server for new messages every 15 minutes).

If you want this, it seems that you need to use AlarmManager, which is not very convenient:

  • Define action for your anxiety
  • Create a receiver (either by creating a dedicated receiver class, either by declaring it as a manifest, or by implementing an interface, registering the receiver with registerReciever and unregistering it)
  • Create an intention for your action.
  • Wrap the indicated intention while waiting for the intention and keep the waiting intention if you want to cancel the alarm
  • Get alarm manager (context is required for this)
  • Set an alarm
  • If you want to cancel the alarm, cancel it using the saved pendingIntent
  • If you decide to have multiple intentions or intentions with data changes, you will have to save them all in order to clear the alarm manager.
0
source share
1 answer

For normal synchronization operations, you can use a handler

Only from the foreground. Any other use of the Handler for a long-term survey is unreliable at best, even ignoring your fears about uptime calculations.

Define action for your anxiety

It's not obligatory. This is not even a good idea.

Create a receiver (either by creating a dedicated receiver class, either by declaring it as a manifest, or by implementing an interface, registering the receiver with registerReciever and unregistering it)

If you want the event to occur when you are not in the foreground and the alarm goes off when the device is sleeping ( _WAKEUP alarms), a registered receiver is required. If _WAKEUP not required, service will be sufficient. If you only need to be in the foreground and receive an event in action, use createPendingResult() to give you a PendingIntent that will call onActivityResult() your activity. Although in this latter case it would be wiser to use postDelayed() for a View or Handler .

and keep the pending intent if you want to cancel the alarm

If saving the PendingIntent is an option, then the Handler is all you need and the AlarmManager unusable. To cancel the alarm, you need the equivalent of PendingIntent (the one where the main Intent objects correspond based on filterEquals() and where the PendingIntent operation [activity, service, broadcast] is the same).

If you want to cancel the alarm, cancel it using the saved pendingIntent

No, you cancel it by creating an equivalent PendingIntent .

If you decide to have multiple intentions or intentions with data changes, you will have to save all of them in order to subsequently clear the alarm manager

No, you cancel them by creating the equivalent of PendingIntent .

Is there an easy way to delay the exectuion based on the "wallclock", for example, through the open source shell around the AlarmManager?

Creating a wrapper code for this, covering 80% of the work, would take less time than writing your question. I do not know about a separate library for this, partly because there would not be so many.

Or use ScheduledExecutorService and WakeLock for short-term things. This is not suitable for β€œevery 15 minutes” scenarios, as it constantly wakes up the device.

0
source

All Articles