Is Android Wakelock a must for background services?

I am writing a long background service that collects and analyzes device data. To do this, I start the service, the service spawns a stream, and the stream collects / analyzes data.

From my reading, I learned that if I want the service to do this while the screen is locked / the device is sleeping, I have to use a partial wakelock to ensure that the processor does not fall asleep while the service is running. However, I am really confused by this, because currently I am not using wakelock and cannot use my application or lock the screen or kill the application through the task manager, and every time I do this, I can observe through logcat and Log statements that the thread that I spawned in the service still works and does the work that I want.

I even ran ADB shell dumpsys power and found that I have RefCount = 0 for wakelocks, which tells me that another application does not hold wakelock for me.

Am I just lucky that my service seems to be working fine in the background without wakelock?

Edit: The device displayed this behavior when connected to USB and when disconnected from USB. I periodically upload the data that I collect to the server and run tests in which I never connect the device and close the screen the whole test, and I still see that my stream is working and the data is loading.

+6
source share
1 answer

You are not lucky that your service is beautiful without wakelock. Your device has never slept.

If you plugged in USB and turned on the power, your device will not sleep. After all, what is the point of saving energy if you have a ready-made supply for its use?

You need to connect to your device using adb over IP (adb connect) and then logcat from there to see if you are all watching the expected logs.

EDIT: Forgot to answer the question.

If you want your service to constantly do something, you need to keep wakelock. Keep in mind that wakelock persistent storage is basically the No. 1 sin in mobile programming. You must NEVER CONSTANT PERMANENT PROTECTION .

Since your service monitors data usage, then there is no need for wakelock, because if the device is sleeping, then there is no data to register.

EDIT 2: Let the experiment!

Hmmm. Sleep is hard to get, so let go directly to the nuclear option.

  • Modify your code so that instead of actually loading data, you will save some metadata instead to let you know that you saved data with a specific timestamp. Also, change your code so that you can track power events, and mark them with a time stamp. If you can, save all this data in memory so that we don’t have too much system code.

  • Put your phone in airplane mode. Right, disconnect all external connections. Also, do not use ADB or logcat or anything like that.

  • Kill all unnecessary applications. Remove is better.

  • Start the service as the user will use the user interface. DO NOT USE ANY VARIETY TEST; TESTS CAN PREVENT DREAM (it's hard to know anything for sure on Android, because drowsiness is an OEM thing).

  • Wait a while.

  • Reset your time stamped logs. You probably will not get accurate data about when the dream occurred, even if it even tells you that you were sleeping. But you should be able to get it out of the odd order of power events when you move on to the state you were in.

+4
source

Source: https://habr.com/ru/post/986474/


All Articles