wifiLock and wakeLock not working correctly on Android

前端 未结 3 1530
[愿得一人]
[愿得一人] 2020-12-20 23:06

I am developing an app that needs to use both wifiLock and wakeLock so the audio streaming when the screen is off does not get disturbed. I have tried my app on Android 2.3

3条回答
  •  天命终不由人
    2020-12-20 23:44

    It is so late to post the answer but maybe it helps someone. As i faced the same problem and got the solution as :

            WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "MyWifiLock");
            wifiLock.acquire();
            PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
            wakeLock.acquire();
    

    And release the locks in destroy method:

    if (wakeLock != null) {
                if (wakeLock.isHeld()) {
                    wakeLock.release();
                }
            }
            if (wifiLock != null) {
                if (wifiLock.isHeld()) {
                    wifiLock.release();
                }
            }
    

    What i think is missing in your case is you're not acquiring locks. See this link.

提交回复
热议问题