Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, so I've found the answer, I think my question was a little convoluted to begin with, so I'll try to answer the various parts, as well as adding my actual solution....</p> <p>I had an <code>Activity</code> that was starting a <code>Service</code>. The <code>Service</code> (which included an <code>AsyncTask</code>) was supposed to run in the background, monitoring the accelerometer and counting down a timer. The <code>Service</code> was then periodically sending a broadcast <code>Intent</code> to the <code>Activity</code>, when the broadcast contained certain data, the <code>Activity</code> needed to run some code. This all needed to happen with the screen off.</p> <p>My findings are that, yes, when hitting the screen lock, the <code>Activity</code> does run <code>onPause()</code>, but is still fully able to execute code in the internal <code>BroadcastReciever</code> class, and consequently this can call methods in the activities main process. I imagine this is a bad way to do things, but it did work.</p> <p>The issue was with the <code>AsyncTask</code> and the <code>Service</code>. Regardless of the the way it's set up, (<code>startForegrond()</code> etc ) a background thread will be paused for a while when the cpu shuts down (either by pressing the lock key, or waiting for the screen to time out). I found it was paused for intervals of about 1 min.</p> <p>The answer is to use <code>PowerManager.WakeLock</code> and set the flag to <code>PARTIAL_WAKE_LOCK</code>. This will allow the screen to shut down but keep threads running at full speed in the background.</p> <p>Doc here: <a href="http://developer.android.com/reference/android/os/PowerManager.html" rel="nofollow">http://developer.android.com/reference/android/os/PowerManager.html</a> </p> <p>dont forget to release it when done as it is expensive on the battery.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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