wifiLock和激活锁定无法在Android上正常工作 [英] wifiLock and wakeLock not working correctly on Android

查看:994
本文介绍了wifiLock和激活锁定无法在Android上正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个需要同时使用wifiLock和激活锁定使音频流时,屏幕关闭时没有得到不安的应用程序。
我已经试过我的应用程序在Android 2.3及激活锁定,看起来像wifiLock按预期方式工作所以当屏幕开启或关闭时没有任何区别,但是在Android 4.2(果冻豆)相同的应用程序时,屏幕熄灭不工作以及和音频变得波涛汹涌这表明无论是激活锁定或wifiLock无法正常工作。有没有道理呢?

下面是我如何获取和释放锁,在我的主要活动我有:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    电源管理电源管理=(电源管理)getSystemService(Context.POWER_SERVICE);
    激活锁定= powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCKMyWirelessHeadphone);    WifiManager WM =(WifiManager)getSystemService(Context.WIFI_SERVICE);
    wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULLMyWifiLock);
    ...
}@覆盖
保护无效的onDestroy(){
    super.onDestroy();
    如果(wakeLock.isHeld()==真)
        wakeLock.release();
    如果(wifiLock.isHeld()==真)
        wifiLock.release();
}


解决方案

我刚刚碰到这个问题绊倒 - 可能是几年为时已晚的OP,但以防万一它可以帮助别人,你可能需要使用<一href=\"http://developer.android.com/reference/android/net/wifi/WifiManager.html#WIFI_MODE_FULL_HIGH_PERF\"相对=nofollow> WIFI_MODE_FULL_HIGH_PERF 而不是 WIFI_MODE_FULL ,因为这可能会导致流问题:

  wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERFMyWifiLock);

我可能会看流在服务中的音频代替活动过,尤其是当你似乎在后台运行的理念来设计它。

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 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. 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:

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.

这篇关于wifiLock和激活锁定无法在Android上正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆