为什么这段代码在 android 5.0中不起作用?
我应该调用什么方法在 Android 5.0中打开/关闭它?
WifiConfiguration wificonfiguration = new WifiConfiguration();
wificonfiguration.SSID = "Wifi Hotspot";
wificonfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wificonfiguration.preSharedKey = "123";
WifiManager mWifiManager;
mWifiManager = (WifiManager) this.context1.getSystemService(Context.WIFI_SERVICE);
try {
if (mWifiManager.isWifiEnabled()) { // disable WiFi in any case
mWifiManager.setWifiEnabled(false);
}
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, wificonfiguration, true);
//Toast.makeText(context, "OK", 0).show();
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
}
添加清单:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>