App seems to stop working when the screen goes to sleep

后端 未结 3 2058
离开以前
离开以前 2020-12-05 15:59

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 percen

相关标签:
3条回答
  • 2020-12-05 16:39

    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

    0 讨论(0)
  • 2020-12-05 16:43

    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.

    0 讨论(0)
  • 2020-12-05 16:58

    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.

    0 讨论(0)
提交回复
热议问题