切换无线网络共享编程中的Andr​​oid 4.2无线网络、Andr、oid

2023-09-05 10:57:24 作者:岁月并非如歌

是否有可能切换无线网络共享中的Andr​​oid 4.2?我尝试this但似乎,它并没有使用Android的工作4.2!

Is there any possibility to toggle wifi tethering in Android 4.2? I tried this but it seems that it doesn't work using Android 4.2!

在此先感谢!

编辑:它的工作,我只是忘了设置正确的权限。有没有还的方法来检查其是否已经启用,这样我可以把它作为一个切换?

It DOES work I only forgot to set the right permission. Is there also a way to check if its already enabled so that I can use it as a toggle?

推荐答案

您应该能够做到这一点在安卓4.2请确保您有权限

You should be able to do it in Android 4.2 make sure you have the permission

android.permission.CHANGE_WIFI_STATE

和我们帮不了你,除非你发表您的code。

and we cannot help you unless you post your code.

我相信这将帮助你检查网络共享处于活动状态

I believe this will help you check if tethering is active

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();
    }
  }
}