如何检测如果网络(配置)在Android移动热点? [英] How to detect if a network is (configured as) a mobile hotspot on Android?

查看:230
本文介绍了如何检测如果网络(配置)在Android移动热点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Android的4.1,设备可以当它连接到一个移动热点(鉴于移动热点还运行Android 4.1或更高版本)检测。此外,您还可以选择标志网络作为移动热点(在​​设置/数据使用/溢出菜单/移动热点)。

As of Android 4.1, your device can detect if it's connected to a mobile hotspot (given that the mobile hotspot is also running Android 4.1 or higher). Also, you have the option to flag networks as mobile hotspots (under Settings / Data Usage / Overflow menu / Mobile Hotspots).

但我怎么发现这是一个-user-我的意思是开发者?这不是存储在WifiConfiguration,所以它在哪里?

一些背景:我想建立Android一个简单的工具来检查,如果您连接到您或Android已经标记为移动热点的网络。如果是这样,它会检查是否没有其他(非热点)网络是可用的。如果是这样,它应该连接到因为与这些其它网络要快很多,并且没有数据帽。为什么?因为我的手机和平板电脑连接到(移动)热点经常,甚至当一个更好的网络是可用的。

Some context: I want to build a simple tool for Android that checks if you are connected to a network that you or Android has flagged as a mobile hotspot. If so, it will check if no other (non-hotspot) networks are available. If so, it should connect to these other networks since those should be much faster and have no data cap. Why? Because my phones and tablets connect to (mobile) hotspots quite often, even when a better network is available.

下面是一些伪code就是我在寻找:

Here is some pseudo code of what I'm looking for:

// Check if android has detected mobile hotspot
WifiManager wifiMgr = getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr .getConnectionInfo();
boolean isMobileHotspot = wifiInfo.isMobileHotspot;

更新2014年7月3日

好了,所以Matiash的回答是好的,但ConnectivityManager.isActiveNetworkMetered()将只对当前网络返回值。我确实需要这一点,所以它一起帮助我,但它带给我的下一个部分。我的工具/应用程序:

Okay so Matiash' answer is good but ConnectivityManager.isActiveNetworkMetered() will only return the value for the current network. I do need that, so it helped me along, but it bring me to the next part in my tool/app:

如果将设备连接到移动热点(或计量网络为Android把它)我要检查是否有附近的接入点是一个更好的选择。所以,我需要知道是否有任何已知的AP的(WifiManager.getConfiguredNetworks())中也被标记为这样之前,我连接到​​它...

IF the device is connected to a mobile hotspot (or a 'metered network' as Android calls it) I want to check if any of the nearby access points is a better option. So I need to know whether any of the known AP's (WifiManager.getConfiguredNetworks()) is also flagged as such before I connect to it...

我有一个列表< ScanResult> 列表< WifiConfiguration> ,貌似他们都不具有这种信息。

I have a List<ScanResult> and a List<WifiConfiguration>, looks like neither of them has this information.

这让我回到我最初的问题:有没有一种方法来检索移动热点在数据使用(如配置由Android和/或用户)?而这一次我的意思是他们。

Which bring me back to my initial question: Is there a way to retrieve the Mobile Hotspots (as configured by Android and/or user) under Data Usage? And this time I mean ALL of them.

更新2014年7月7日

我已经张贴在AOSP问题跟踪访问(只读)功能请求NetworkPolicyManager。 PLZ在这里就可以了表决:的https://$c$c.google.com/p/android/issues/detail?id=73206&thanks=73206&ts=1404719243

I've posted a feature request in the AOSP Issue Tracker for access (readonly) to the NetworkPolicyManager. Plz vote on it here: https://code.google.com/p/android/issues/detail?id=73206&thanks=73206&ts=1404719243

推荐答案

您可以通过调用<一个访问该信息href=\"http://developer.android.com/reference/android/net/ConnectivityManager.html#isActiveNetworkMetered%28%29\"相对=nofollow> ConnectivityManager.isActiveNetworkMetered()

You can access this information by calling ConnectivityManager.isActiveNetworkMetered().

这将返回有效连接是否是一个热点(如数据使用量的定义 - >移动热点)。

This will return whether the active connection is a hotspot (as defined in Data Usage -> Mobile Hotspots).

关于第二部分,我很抱歉,但我不认为这是可能的。该标志是不公开的,即使你得到了可用于通过反射来检索它( android.net.NetworkPolicyManager )对象:

About the second part, I'm sorry but I don't think that's possible. The flag is not public, and even if you get the object that could be used to retrieve it (android.net.NetworkPolicyManager) by reflection:

Object npm = Class.forName("android.net.NetworkPolicyManager").getDeclaredMethod("from", Context.class).invoke(null, this);
Object policies = npm.getClass().getDeclaredMethod("getNetworkPolicies").invoke(npm);

调用 getNetworkPolicies()要求 MANAGE_NETWORK_POLICY 许可,其中的不能获得人非系统的应用程序,因为它有一个签名保护级别。

calling getNetworkPolicies() requires the MANAGE_NETWORK_POLICY permission, which cannot be obtained by non-system apps, because it has a "signature" protection level.

我希望能证明不正确,但。 :)也许在寻找管理该信息的Andr​​oid活动的源头code(<一个href=\"https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/net/DataUsageMeteredSettings.java\" rel=\"nofollow\">https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/net/DataUsageMeteredSettings.java),特别是 buildWifi preF()方法,将提供一些线索。

I hope to be proved incorrect though. :) Maybe looking at the source code of the Android activity that manages this information (https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/net/DataUsageMeteredSettings.java), in particular the buildWifiPref() method, will provide some clue.

这篇关于如何检测如果网络(配置)在Android移动热点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆