Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your first solution (widget handling screen ON and OFF broadcasts) to the problem is the way to go. Start a new <a href="http://developer.android.com/reference/android/app/IntentService.html" rel="nofollow noreferrer">IntentService</a> for a background communication to your application server. Once it finished, send your custom "Completed" broadcast with results and then handle it in your widget. </p> <p>As an additional benefit it would allow multiple instances of your widget to work from one "source" and would not consume any resources in case user did not add any widgets.</p> <p><strong>UPDATE</strong> As it is impossible to use screen ON/OFF broadcasts with a widget, I would probably use <a href="https://stackoverflow.com/a/4545610/181714">this</a> (AlarmManager with an <code>ELAPSED_REALTIME</code> (not <code>ELAPSED_REALTIME_WAKEUP</code>) alarm) to schedule an (intent) service run. This way your service will not be scheduled when screen is off, but will run periodically when it is on. And if the delay between screen ON and OFF is more than the period it will run immediately on screen coming back ON.</p> <p>As for your question about broadcast receivers lifetime, as said before, use IntentService for a long running task and then broadcast your results back from it. When you starting a service that call does not block so broadcast receiver will finish in a timely matter.</p> <p><strong>UPDATE 2</strong> According to <a href="https://groups.google.com/d/msg/android-developers/Airlo3g5luw/OcLEUW2aq6UJ" rel="nofollow noreferrer">this</a> screen OFF does not mean device is sleeping. You already have a solution to this issue by using PowerManager.isScreenOn().</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload