获取 Android 的网络信息,需要使用 ConnectivityManager 这个类。
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).
The primary responsibilities of this class are to: 1. Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
Send broadcast intents when network connectivity changes
Attempt to "fail over" to another network when connectivity to a network is lost
Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
ConnectivityManager mManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] infos = mManager.getAllNetworkInfo();
通过上面的代码,能获取到一个 NetworkInfo 的数组,除了咱们关心的 wifi 和 mobile 外,还有一些特殊的网络链接。
- EOF -
本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。
转载请注明:文章转载自 Binkery 技术博客 [https://binkery.com]
本文标题: Android 所有的网络信息 NetworkInfo
本文地址: https://binkery.com/archives/360.html