[英]Toggle wifi tethering programmatically in Android 4.2
有可能在Android 4.2中切換wifi網絡共享嗎? 我試過了,但似乎無法在Android 4.2上使用!
提前致謝!
編輯:確實可以,但我只忘記設置正確的權限。 還有一種方法可以檢查它是否已啟用,以便可以將其用作切換?
您應該能夠在Android 4.2中做到這一點,請確保您具有權限
android.permission.CHANGE_WIFI_STATE
除非您發布代碼,否則我們無法為您提供幫助。
我相信這將幫助您檢查網絡共享是否處於活動狀態
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for(Method method: wmMethods){
if(method.getName().equals("isWifiApEnabled")) {
try {
method.invoke(wifi);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
我自己操作它就可以了! 這是代碼:
WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
boolean enabled=false;
for (Method method : methods) {
if (method.getName().equals("isWifiApEnabled")) {
try {
enabled = (Boolean) method.invoke(wifiManager);
} catch (Exception ex) {
}
break;
}
}
for (Method method : methods) {
if (method.getName().equals("setWifiApEnabled")) {
try {
method.invoke(wifiManager, null, !enabled);
} catch (Exception ex) {
}
break;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.