Service, WakeLock

孤人 提交于 2019-11-28 00:45:27

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.

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" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!