Search code examples
androidstreamingwakelockforeground-service

Music streaming, Google Developer Console warning: hanging partial wakelock


I have built an online clock radio for Android which also features an integrated music player that works as a foreground service with notification, and it has its own wakelocks - acquiring when service is started and releasing when the user presses "stop" and finishes the service.

Now I get a warning about hanging partial wakelocks for this app in Google Developer Console. It's absolutely correct to receive this warning, because if someone keeps listening to music for a longer period of time, then also the wakelocks (wifi-manager and power-manager) will be held as long as needed to keep the music playing when the screen is off.

The app works perfectly and plays for hours when the screen is off and/or in power save mode - exactly as intended.

Also: In Developer Guidelines and best practices they state:

If you must use partial wake locks, follow these recommendations:

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.

Make sure the logic for acquiring and releasing wake locks is as simple as possible. When your wake lock logic is tied to complex state machines, timeouts, executor pools, and/or callback events, any subtle bug in that logic can cause the wake lock to be held longer than expected. These bugs are difficult to diagnose and debug.

I think I have taken care of this.

They tell me in dev console where the warning appears:

Keep in mind that for some apps, longer wakelocks are required to enable key features such as music streaming.

Due to that I believe everything is fine, but since the warning for "bad behavior" is still shown I would like to get advice.

The questions are:

  1. Can I do anything to avoid getting this warning?
  2. Will I have to fear "punishment" by Google for my app showing "bad behavior" although it's working correctly?

Streaming-service (partial) code:

    @Override
        public void onCreate() {
    
            ...
    
        // WAKE LOCK && WIFI LOCK
            powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
            wakeLock.acquire();
            
            wMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            wifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock");
            wifiLock.acquire();
            
            ...
        // notification gets built
            ...

        startForeground(ntfctnId, buildNotification(standardNoti));
    
     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
        // player here...
    
            ...
    
    @Override
        public void onDestroy() {
            
            ...
    
        // RELEASE WAKE LOCK && WIFI LOCK
            wakeLock.release();
            wifiLock.release();
    
            ...

Solution

  • I have not noticed anything negative - neither from Google, nor users, nor anything or anyone else.

    Everything is just fine. If wakelocks cannot be avoided - and that's clearly the case for music apps - you do not need to fear any related warnings about this in the Developer Console.