Android service stops working when phone locked

后端 未结 3 779
轻奢々
轻奢々 2021-01-12 08:06

I have an application which starts a service. The service has a timer and pops a notification every 30 seconds. I notice the following behavior:

  • if the screen
3条回答
  •  情话喂你
    2021-01-12 08:27

    I had the some problem.

    You may use the WakeLock to do the work! Something like this:

    PowerManager pm = (PowerManager)getApplicationContext().getSystemService(
              getApplicationContext().POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wl.acquire();
    

    Then use this:

    wl.release();
    

    to release the wakelock.

    Don't forger to add:

    to the manifest!

提交回复
热议问题