简体   繁体   English

wifiLock和wakeLock在Android上无法正常工作

[英]wifiLock and wakeLock not working correctly on Android

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. 我正在开发一个需要同时使用wifiLock和wakeLock的应用程序,因此屏幕关闭时的音频流不会受到干扰。 I have tried my app on Android 2.3 and wakeLock and looks like wifiLock work as expected so there is no difference between when the screen is on or off, but the same app on Android 4.2 (Jelly-bean) when the screen goes off is not working as well and the audio gets choppy which shows either wakeLock or wifiLock are not working correctly. 我在Android 2.3和wakeLock上尝试了我的应用程序,看起来像wifiLock正常工作,所以屏幕打开或关闭时没有区别,但屏幕熄灭时Android 4.2(Jelly-bean)上的相同应用程序不是同样工作,音频变得波动,显示wakeLock或wifiLock无法正常工作。 Is there any reason for this? 这有什么理由吗?

Here is how I acquire and release the locks, in my main activity I have: 以下是我获取和释放锁的方法,在我的主要活动中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyWirelessHeadphone");

    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
    ...
}



@Override
protected void onDestroy() {
    super.onDestroy();
    if (wakeLock.isHeld()==true) 
        wakeLock.release();
    if (wifiLock.isHeld()==true) 
        wifiLock.release();
}   

I only just stumbled across this question - probably a couple of years too late for the OP, but just in case it helps someone else, you probably need to use WIFI_MODE_FULL_HIGH_PERF instead of WIFI_MODE_FULL , as that can cause streaming issues: 我只是偶然发现了这个问题 - 可能对OP来说已经太晚了几年,但为了防止其他人,你可能需要使用WIFI_MODE_FULL_HIGH_PERF而不是WIFI_MODE_FULL ,因为这会导致流媒体问题:

wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "MyWifiLock");

I'd probably look at streaming the audio in a Service instead of an Activity too, especially as you appear to be designing it with the idea of running in the background. 我可能会考虑在Service而不是Activity中流式传输音频,尤其是因为您似乎正在设计它以在后台运行的想法。

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: 并在destroy方法中释放锁:

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 . 看到这个链接

You are acquiring the lock in an activity and when your activity goes in the stack the system can remove and destroy your activity and this maybe the cause of your problem. 您正在获取活动中的锁定,当您的活动进入堆栈时,系统可以删除并销毁您的活动,这可能是您的问题的原因。 Try to acquire the lock in a service. 尝试获取服务中的锁定。 But the combination of PARTIAL lock and WIFI does not work properly on some devices as well see this question: Android WifiLock not working? 但PARTIAL锁和WIFI的组合在某些设备上无法正常工作,也看到了这个问题: Android WifiLock无法正常工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM