Service, WakeLock

后端 未结 2 576
我寻月下人不归
我寻月下人不归 2020-12-11 01:09

I am bit confused after going through the questions & answers in Stackoverflow about WakefulIntentService. I just would like to get some knowledge on this t

相关标签:
2条回答
  • 2020-12-11 01:31

    What I am hearing from this forum, the service might turn off as soon the device goes to asleep. Is that true?

    Yes.

    In my case, it works always

    Then something else on your device is keeping the device from falling asleep. Perhaps use adb shell dumpsys power to see what WakeLocks are outstanding.

    What is the need of WakeFulIntent Service? When do we need to use WakefulIntentService?

    The device may fall asleep if the user is inactive and nothing is keeping the device awake. A WakeLock is used to ensure the device stays awake. For transactional-type work (e.g., downloading a file), WakefulIntentService combines an IntentService and a WakeLock to make keeping the device awake as long as necessary (and only as long as necessary) relatively easy.

    WakefulIntentService is not suitable for use with services that need to run indefinitely, such as a music player. For those, manage your own WakeLock.

    0 讨论(0)
  • 2020-12-11 01:52

    I used the code below in an app.

    Make sure your service is sticky:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        //this service will run until we stop it
    
        return START_STICKY;
    }
    

    I you want your phone to be awake constantly u can use this code below:

    private WakeLock wl;
    
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "whatever");
        wl.acquire();
    

    Don't forget the permissions in your manifest.

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    0 讨论(0)
提交回复
热议问题