我尝试使用此方法在设备上启动wifi服务:
public void wifiActivation() {
Context context = MainActivity.this;
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
//Boolean checkWifiStatus = wifiManager.isWifiEnabled();
/*if(!checkWifiStatus ){*/
try
{
Boolean checkWifiStatus = wifiManager.isWifiEnabled();
wifiManager.setWifiEnabled(true);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
但我在布尔值上检测到错误' checkWifiStatus',我猜它是关于权限的:
java.lang.SecurityException: WifiService: Neither user 10126 nor current process has android.permission.ACCESS_WIFI_STATE.
但是在我的清单中你可以找到这些权限:
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Wifi State Permissions -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
答案 0 :(得分:2)
除了您现有的权限外,还添加以下权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />