Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 my understanding is correct, please feel free to correct me, if I am wrong.

I built a small application, where I am using a background Service that keeps playing music whenever the user shakes the mobile. I tested after the device is locked and screen is turned off and it works as expected.

  1. What I am hearing from this forum, the service might turn off as soon the device goes to asleep. Is that true? In my case, it works always, Am I missing something?

  2. What is the need of WakeFulIntentService? When do we need to use WakefulIntentService?

  3. I tried running a timer in a Service, though the device is locked and screen is turned off and my timer is running pretty much I can say for sure. Because I used to get notification whenever my timer trips.

like image 495
Ramesh Sangili Avatar asked Jan 02 '13 19:01

Ramesh Sangili


People also ask

What is a Wakelock on Android?

A wake lock is a mechanism to indicate that your application needs to have the device stay on. Any application using a WakeLock must request the android. permission. WAKE_LOCK permission in an <uses-permission> element of the application's manifest. Obtain a wake lock by calling PowerManager#newWakeLock(int, String) .

What is Wakelock detector?

Wakelock Detector” helps you to detect battery consuming applications in your Android device by checking wakelock usage history. Now you can find out which applications drain your battery in a simple way by using this app!

How do you release a Wakelock?

To release the wake lock, call wakelock. release() . This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.

What is Termux Wakelock?

termux-wake-lock and termux-wake-unlock can be used to trigger a wakelock which causes Android not to go into deep sleep. Since the default login shell is bash; it could be useful to call termux-wake-lock in your ~/. profile (or ~/. bash_profile where appropriate) and termux-wake-unlock in your ~/.


2 Answers

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.

like image 96
CommonsWare Avatar answered Oct 13 '22 23:10

CommonsWare


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" />
like image 11
Jasper Fioole Avatar answered Oct 14 '22 01:10

Jasper Fioole