有没有办法检查网络共享是否处于活动状态?

发布于 2024-12-13 19:07:34 字数 119 浏览 5 评论 0原文

我可以通过编程检查 Android 设备是否已激活网络共享吗?

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

谢谢, 此致

can I check programmaticall wheter the android device has tethering activated?

I just watched the WifiManager class. All vatraibles from the WifiInfo show the same values as iff the WIFI is turned off on the device.

Thnaks,
best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2024-12-20 19:07:34

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

First, you need to get WifiManager:

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

Then:

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

Also you need to request a permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
流心雨 2024-12-20 19:07:34

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

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


正如 Dennis 建议的那样,最好使用这个:(

    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

Try using reflection, like so:

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

(It returns a Boolean)


As Dennis suggested it is better to use this :

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

(manager is the WiFiManager)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文