在 Android 中查找可用的 wifi 连接
Find available wifi connections in Android
我正在尝试创建一个帮助向导以从应用程序中的不良网络连接中恢复。我希望处理的一个测试案例是终端用户关闭 WiFi,WiFi 可用,但移动网络比 WiFi 网络慢的情况。在这个事件中,我希望能够 (1) 发现可用的 WiFi 网络,(2) 找到 WiFi 网络速度,(3) 将其速度与移动网络速度进行比较,(4) 消化用户对更快的网络。
为此,我需要知道如何以编程方式获取有关可用连接的信息。这是我们能做的吗?如果是这样,我们如何知道哪些连接可用?提前致谢。
任务 1:发现可用的 WiFi 网络
这可以通过从系统获取 WifiManager 的实例来完成。
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
任务2&3:求网速
This link回答了你关于如何获取wifi和移动网络的网络速度的问题
无线网络:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
如果是移动设备,它应该可以工作:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi
Task-4:切换到更高速度的选项
在 Android 默认情况下,如果 wifi 已打开并已连接,则不会使用您的移动网络。因此,要使用移动数据,您必须断开所有可用的 wifi 网络或关闭 wifi。
我还建议您阅读 link this, this and this 以获取有关如何获得连接速度的更多信息。
我正在尝试创建一个帮助向导以从应用程序中的不良网络连接中恢复。我希望处理的一个测试案例是终端用户关闭 WiFi,WiFi 可用,但移动网络比 WiFi 网络慢的情况。在这个事件中,我希望能够 (1) 发现可用的 WiFi 网络,(2) 找到 WiFi 网络速度,(3) 将其速度与移动网络速度进行比较,(4) 消化用户对更快的网络。
为此,我需要知道如何以编程方式获取有关可用连接的信息。这是我们能做的吗?如果是这样,我们如何知道哪些连接可用?提前致谢。
任务 1:发现可用的 WiFi 网络
这可以通过从系统获取 WifiManager 的实例来完成。
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
任务2&3:求网速
This link回答了你关于如何获取wifi和移动网络的网络速度的问题
无线网络:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
如果是移动设备,它应该可以工作:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi
Task-4:切换到更高速度的选项
在 Android 默认情况下,如果 wifi 已打开并已连接,则不会使用您的移动网络。因此,要使用移动数据,您必须断开所有可用的 wifi 网络或关闭 wifi。
我还建议您阅读 link this, this and this 以获取有关如何获得连接速度的更多信息。