音乐流式传输,Google 开发者控制台警告:挂起部分唤醒锁

Music streaming, Google Developer Console warning: hanging partial wakelock

我为 Android 构建了一个在线时钟收音机,它还具有一个集成的音乐播放器,可作为带通知的前台服务工作,并且它有自己的唤醒锁 - 在服务启动时获取并在服务启动时释放用户按下“停止”并结束服务。

现在我在 Google 开发者控制台中收到关于此应用挂起部分唤醒锁的警告。收到这个警告是绝对正确的,因为如果有人长时间听音乐,那么 wakelocks(wifi-manager 和 power-manager)也会被保持,只要需要就可以保持音乐在屏幕上播放已关闭。

该应用程序运行完美,在屏幕关闭时可以播放数小时 and/or 在省电模式下 - 完全符合预期。

还有: In Developer Guidelines and best practices 他们说:

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.

我想我已经处理好了。

他们在开发控制台中告诉我出现警告的位置:

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

因此我相信一切都很好,但由于“不良行为”的警告仍然显示,我想得到建议。

问题是:

  1. 我可以做些什么来避免收到此警告吗?
  2. 我是否会担心 Google 会因为我的应用程序显示“不良行为”而对它进行“惩罚”,尽管它运行正常?

流媒体服务(部分)代码:

    @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();
    
            ...

我没有注意到任何负面消息 - 无论是来自 Google,还是来自用户,也不是来自任何其他人。

一切都很好。如果无法避免唤醒锁 - 音乐应用程序显然就是这种情况 - 您无需担心开发者控制台中关于此的任何相关警告。