Service pauses on screen lock

前端 未结 3 574
说谎
说谎 2021-01-26 19:02

For testing purposes i have made a service that beeps every 1 minute. (No client-server interface yet). It beeps okay when the screen in on, but when it goes to sleep the beepin

相关标签:
3条回答
  • 2021-01-26 19:18

    You have to use the AlarmManager, there are plenty of posts here on stackoverflow.

    0 讨论(0)
  • 2021-01-26 19:19

    You want to acquire a partial wake lock (leaving the CPU running whenever sleep is entered on the device) as suggested by your code.

    The issue is your presumably overriden on start releases the wake lock. You want to release your wakeLock in onDestroy .. once your service is finished running.

    0 讨论(0)
  • 2021-01-26 19:30

    This finally worked for me. Download the CWAC-WakefulIntentService.jar from https://github.com/commonsguy/cwac-wakeful

    add a class in your project

    import com.commonsware.cwac.wakeful.WakefulIntentService;
    
    public class WakeService extends WakefulIntentService {
    
        public WakeService(String name) {
    
            super(name);
    
        }
        @Override
        protected void doWakefulWork(Intent intent) {
        }
    }
    

    now add the following line in your code where ever you want to repeat the loop and wake the device up

    WakefulIntentService.sendWakefulWork(this, S_WS_WakeService.class);

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