有没有办法检查网络共享是否有效?

sof*_*ply 6 android wifi tethering

我可以检查android设备是否激活了网络共享程序?

我刚观看了WifiManager课程.来自WifiInfo的所有视频都显示与在设备上关闭WIFI时相同的值.

Thnaks,最好的问候

Ren*_*eno 8

尝试使用反射,如下所示:

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();
}
}
Run Code Online (Sandbox Code Playgroud)

(它返回一个Boolean)


正如丹尼斯建议最好使用它:

    final Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
    method.setAccessible(true); //in the case of visibility change in future APIs
    return (Boolean) method.invoke(manager);
Run Code Online (Sandbox Code Playgroud)

(经理是WiFiManager)


Den*_*kiy 8

首先,你需要获得WifiManager:

Context context = ...
final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Run Code Online (Sandbox Code Playgroud)

然后:

public static boolean isSharingWiFi(final WifiManager manager)
{
    try
    {
        final Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
        method.setAccessible(true); //in the case of visibility change in future APIs
        return (Boolean) method.invoke(manager);
    }
    catch (final Throwable ignored)
    {
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

您还需要在AndroidManifest.xml中请求权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Run Code Online (Sandbox Code Playgroud)