Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android vitals - Stuck Wake Locks and foreground service

In Developer console - Android vitals I see warning that app has "Stuck partial wake locks". In documentation it's described as

A partial wake lock becomes stuck if it is held for a long time while your app is running in the background (no part of your app is visible to the user).

It's true that app is running in background (playing audio) but I use foreground service and whole time when app holds lock notification is visible.

Later in documentation you can read

Make sure some portion of your app remains in the foreground. For example, if you need to run a service, start a foreground service instead. This visually indicates to the user that your app is still running.

But it doesn't seems to be true. Is Android vitals ignoring that app has foreground service? Does anyone has similar experience?

EDIT: here is code from my AudioService

PowerManager.WakeLock mWakeLock = null;
boolean mHasWakeLock;

void acquireLocks(){

    if (!mHasWakeLock) {
        final PowerManager pm = (PowerManager) 
        context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,TAG);
        mWakeLock.setReferenceCounted(false);
        mWakeLock.acquire();
        mHasWakeLock = true;
    }
}

void releaseLocks() {
    if (mWakeLock != null) {
        mWakeLock.release();
        mWakeLock = null;
    }
    mHasWakeLock = false;
}

void play(){
    startForeground(NOTIFICATION_ID, notification);
    acquireLocks();
}

void stop(){
    releaseLocks();
    stopForeground(true);
}
like image 286
Martin Vandzura Avatar asked Sep 14 '17 09:09

Martin Vandzura


People also ask

How do I stop Wakelocks on Android?

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 stuck partial wake locks?

A partial wake lock becomes stuck if it is held for a long time while your app is running in the background (no part of your app is visible to the user). This condition drains the device's battery because it prevents the device from entering lower power states.

What is a Wakelock on Android?

A wakelock is a powerful concept in Android that allows the developer to modify the default power state of their device. The danger of using a wakelock in an application is that it will reduce the battery life of a device.

What are Android vitals?

Android vitals is an initiative by Google to improve the stability and performance of Google Play apps on Android devices. When an opted-in user runs your app, their Android device logs information about aspects of quality including stability metrics, performance metrics, battery usage, and permission denials.


1 Answers

I think, the problem is in

mWakeLock.setReferenceCounted(false);

Should be true, so the System can count, when you release the lock. In your case, you may well be releasing it, but the System does not "see" it. This might confuse vitals.

Try it with that flag set to true. Hope this helps.

like image 139
Grisgram Avatar answered Sep 22 '22 05:09

Grisgram