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

时间:2011-11-04 09:48:53

标签: android wifi tethering

我可以检查android设备是否已激活绑定的程序设计?

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

Thnaks, 最好的问候

2 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:6)

首先,您需要获得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"/>