Thursday 28 October 2021

Foreground service with PowerManager.PARTIAL_WAKE_LOCK going to sleep

I try to create simple timer. According to documentation I have permission <uses-permission android:name="android.permission.WAKE_LOCK" /> and use this code in the ForegroundService:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getString( R.string.app_name ) );
wl.acquire();

It is working well for most devices when screen goes off, but some devices, for example Honor 10i (Android 10), after 10-15 seconds go to sleep mode and timer based on Handler is sleeping.

I tried set off battery optimization etc., but this is not help. What is wrong?

Note. If function in the Handler plays "tick" sound then it works fine.



from Foreground service with PowerManager.PARTIAL_WAKE_LOCK going to sleep

No comments:

Post a Comment