10

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.

OceanBlue
  • 9,142
  • 21
  • 62
  • 84
  • 2
    "I have a CPU intensive long-running operation (a few hours)" -- this is not exactly a user-friendly use of a battery-powered device. – CommonsWare Mar 17 '11 at 21:26
  • @CommonsWare: "this is not exactly a user-friendly use of a battery-powered device" I completely agree with you. Please see my Edit above for explanation. – OceanBlue Mar 17 '11 at 21:35
  • Take a look at the PowerManager API and the WakeLock method. There's various flags you can call to prevent dimming, locking and sleeping. https://developer.android.com/reference/android/os/PowerManager.html#SCREEN_DIM_WAKE_LOCK – Joshua Pinter May 13 '17 at 22:38

3 Answers3

24

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.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • @EboMike: Thanks for your answer. PowerManager.WakeLock documentation does have this warning "Device battery life will be significantly affected by the use of this API." Does this mean the battery will drain fast & will have to be recharged soon? Or does this mean that the battery life itself will be effected, ergo permanent damage to the device? – OceanBlue Mar 17 '11 at 21:42
  • @Ebomike: "you need to make it clear to the user that your app will drink the phone's milkshake while it's off." I do understand your concern. Please see my Edit to the question above. – OceanBlue Mar 17 '11 at 21:43
  • It means the battery will drain CONSIDERABLY and will have to be recharged SOON. Not permanent damage (although a battery will grow old after many recharges, so if your app causes somebody to charge their phone three times as many times because they use it daily, the battery won't last as long). – EboMike Mar 17 '11 at 21:44
  • @Ebomike: Thanks for your patience. It sounds like there's some basic flaw in my solution. But isn't AsynTask meant for exactly this kind of stuff? To start off a blocking job in another thread & use publishProgress() to update the UI thread?? **If it stops, when the screen goes to sleep what's the point?** – OceanBlue Mar 18 '11 at 02:56
  • Completely unrelated :) AsyncTask is a (fantastic) helper class to allow writing multi-threaded code easily that needs to do things in the UI thread. This has absolutely nothing to do with when the phone turns off. – EboMike Mar 18 '11 at 04:46
  • @Ebomike: +1 & accepting your answer for enlightening me on this. I finally decided not to go the WAKE_LOCK permission way. Sounds too dangerous. I will just use _Screen doesn't timeout while charging_ option or else use set the Screen timeout to _Never_, keep the phone plugged in & run the application. Hope that is an ok solution. Thanks for all your help! – OceanBlue Mar 18 '11 at 14:18
  • Sure! Side note - there is a window flag you can set to "never turn the screen off" in your window flags during onCreate, this doesn't require any permissions and will keep the screen and CPU on while your app is in the foreground. – EboMike Mar 18 '11 at 15:57
7

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) {
    setRetainInstance(true);        
    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.cancel(true);
                connectButton.setText("Start");
                metrics_task.running = false;
                wl.release(); <--releases it on async stop
                }
                else{
                    metrics_task = new start_metrics(ae);
                    metrics_task.execute();
                    wl.acquire(); <--starts it on async start
                }
            }
            else{

                metrics_task = new start_metrics(ae);
                metrics_task.execute();

            }
            }
            else{
                metrics_task = new start_metrics(ae);
                metrics_task.execute();
            }
        }
    });

This worked great with no issues

TwinPrimesAreEz
  • 1,699
  • 1
  • 12
  • 16
  • Thanks for the answer & already upvoted. We tried "partial wake lock" strategy and it works fine. However, we also replaced long `wait()` by intermittent smaller `sleep()`, and that also seemingly working fine. Please see if you can help in this question: [Android: C++ thread not waking up if screen-locked or in background. Works fine when app is in use](http://stackoverflow.com/q/40865524/514235). We are open to wake lock & sleep both, provided some good strategy. – iammilind Dec 01 '16 at 12:23
2

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
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

// do your stuff

// don't forget to re-enable the screen time-out so it won't stay awake from now on
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

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

noamgot
  • 3,962
  • 4
  • 24
  • 44