Skip to content Skip to sidebar Skip to footer

Service, Wakelock

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

Solution 1:

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.

Solution 2:

I used the code below in an app.

Make sure your service is sticky:

@OverridepublicintonStartCommand(Intent intent, int flags, int startId)
{
    //this service will run until we stop itreturn START_STICKY;
}

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

private WakeLock wl;

PowerManagerpm= (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-permissionandroid:name="android.permission.WAKE_LOCK" />

Post a Comment for "Service, Wakelock"