gpt4 book ai didi

java - Android WiFi 状态未知

转载 作者:行者123 更新时间:2023-12-01 12:04:49 26 4
gpt4 key购买 nike

当我尝试返回 wifi 状态、断开连接或连接时,我遇到了问题。好吧,我没有收到异常,但它跳转到 if 条件的 else {} block ,当我调试时,我发现:

 NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

mWifi 有这样的:NetworkInfo:类型:wifi[],状态:未知/空闲,原因:(未指定)

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


if (mWifi.isConnectedOrConnecting()){

Toast.makeText(this,"void action Working", 3).show();



}
else
{
Toast.makeText(this,"void action Not working", 3).show();

}

最佳答案

你可以这样做:

            final WifiManager wifiManager = (WifiManager) YourActivity.this.getSystemService(Context.WIFI_SERVICE); 

if (!wifiManager.isWifiEnabled()) {

wifiManager.setWifiEnabled(true); //If it's, off turn it on.

}else{

//Do what you want to do with wi-fi.

}

另请注意,我在 list 中设置了以下权限(也许您的情况并不需要所有这些权限,因为我正在实现 wifi direct 功能):

  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />

关于java - Android WiFi 状态未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27695169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com