6

我可以以编程方式检查 android 设备是否已激活网络共享?

我刚看了 WifiManager 课。WifiInfo 中的所有变量都显示与设备上关闭 WIFI 时相同的值。

Thnaks,最好的问候

4

2 回答 2

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

(它返回一个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);

(经理是WiFiManager

于 2011-11-04T10:02:28.840 回答
8

首先,您需要获取 WifiManager:

Context context = ...
final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

然后:

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

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

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
于 2013-12-06T19:29:45.770 回答