Home » Android » android – App seems to stop working when the screen goes to sleep

android – App seems to stop working when the screen goes to sleep

Posted by: admin May 14, 2020 Leave a comment


I have a CPU intensive long-running operation (a few hours) that I am using AsyncTask to perform. As it continues, it updates a progressbar on the screen to show what percentage of the task is done.

I discovered that when my screen goes to sleep (time-out) the task seems to stop. Not sure whether this is happing because the AsyncTask stops or it gets stuck at trying to update the screen (latter I am thinking).

Other than never letting the screen sleep, how else can I prevent my AsyncTask to stop executing? And if that is the only way, then how do I make sure that the screen doesn’t sleep?

EDIT: I must add that I know this sounds like a non-user-friendly app as commented by someone below. This does a very specialized task (processes thousands of image files to compare processing on different systems) and is to be used by a few users internally, not for public release.

How to&Answers:

That’s expected behavior. The idea is that the phone’s battery is not supposed to drain because of bad apps. If the screen is off, the user generally expects the phone to sleep.

If you need your app to run, you can use a WakeLock to keep the phone running (with the screen off): Documentation here and here.

Note that a wake lock requires the WAKE_LOCK permission, and again, you need to make it clear to the user that your app will drink the phone’s milkshake while it’s off.


Not sure if anyone will read this as the OP is several years old but I am in the same boat in that I need to use a wakelock for an app for internal use, and leaving the screen on was not ok (I just needed the cpu on so I could run some metrics queries) I simply used a partial wakelock; ie:

public class my_frag extends Fragment {
    WakeLock wl; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    PowerManager pm = (PowerManager) this.getActivity().getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");  

    //I happen to have it in a button click event based on an async task 
    //Side note: I should probably be using a Loader for my Async task but this works fine 
    connectButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (metrics_task != null)
            Status s = metrics_task.getStatus();
            if (s.name().equals("RUNNING")){
                if (metrics_task.running){
                metrics_task.running = false;
                wl.release(); <--releases it on async stop
                    metrics_task = new start_metrics(ae);
                    wl.acquire(); <--starts it on async start

                metrics_task = new start_metrics(ae);

                metrics_task = new start_metrics(ae);

This worked great with no issues


My 2 cents on this old post, in case it might help someone. If all you want is to prevent your screen from going to sleep, there’s a simple solution that does not require a permission from the user – and it’s just 2 lines of code:

// prevent the screen from sleeping

// do your stuff

// don't forget to re-enable the screen time-out so it won't stay awake from now on

An important note: it can only be called within an Activity – not from other app components. more details can be found here.