Android獲取網絡狀態
阿新 • • 發佈:2018-10-08
service 警告 clas enc 流量 https undle when urn 類來實現,該類負責與
該類的主要責任是:
Android獲取網絡狀態
學習自
- https://developer.android.google.cn/reference/android/net/ConnectivityManager
- http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html
Overview
在做Android app的時候,網絡的狀態可能會經常的變化,當用戶當前沒有網絡的時候,我們要保證程序不崩潰,而在當用戶使用流量的話,則需要確保,在進行耗流量的操作的時候,需要給予用戶警告(比如看視頻,播放音樂,下載等).
想要獲取網絡狀態的話,我們需要通過 ConnectivityManager
NetworkManagementService
系統服務進行交互來獲取網絡相關的信息。我們先來看一下官方文檔是如何描述這個類的:
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes.
該類負責回答關於網絡連接的狀態的問題。 當網絡連接發生改變的時候也會通知應用。
The primary responsibilities of this class are to:
- Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
- 監聽網絡連接(Wifi,GPRS,UMTS等)
- 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
- 提供一個API來允許應用查詢粗粒度或細粒度的可用網絡的狀態
- Provide an API that allows applications to request and select networks for their data traffic
- 提供一個API允許應用為他們的數據流量請求或者選擇網絡
可以通過下面的方式獲取此類的實例
val connectivityManager: ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val connectivityManager: ConnectivityManager =
context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager
小貼士,請不要忘記添加
android.permission.ACCESS_NETWORK_STATE
權限。
NetworkInfo
NetworkInfo 是一個描述網絡連接狀態的接口,我們主要通過這個類來獲取網絡的狀態。
val connectivityManager: ConnectivityManager =
context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
常用的功能
判斷網絡是否連接
/**
* 判斷網絡是否可用
* */
fun isAvailable(context: Context): Boolean {
val connectivityManager: ConnectivityManager =
context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null) {
return networkInfo.isAvailable
}
return false
}
判斷是否是移動數據
fun isMobile(context: Context): Boolean {
val connectivityManager: ConnectivityManager =
context.getSystemService(ConnectivityManager::class.java) as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null) {
return networkInfo.type == ConnectivityManager.TYPE_MOBILE
}
return false
}
監聽網絡的變化
從官方文檔我們可以知道,如果要想監聽到網絡的變化的,那麽需要我們動態註冊一個廣播才行。
val networkStatusChangeReceiver = object : BroadcastReceiver() {
@SuppressLint("ServiceCast")
override fun onReceive(context: Context?, intent: Intent?) {
val connectivityManager: ConnectivityManager =
context!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null && networkInfo.isAvailable) {
tvNetworkStatus.text = "Current Network Status: Available"
} else {
tvNetworkStatus.text = "Current Network Status: unavailable"
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//註冊一個廣播
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
this.registerReceiver(this.networkStatusChangeReceiver, filter)
}
override fun onDestroy() {
this.unregisterReceiver(this.networkStatusChangeReceiver)
super.onDestroy()
}
Android獲取網絡狀態