Android开发实时监听网络状态变化一般有两种方法:
1. 新建一个基类BasicActivity.class,在基类中注册网络监听广播NetworkChangeReceiver.class,所有页面的Activity都继承此基类BasicActivity.class
(1)网络状态改变监听类:NetWorkChangeReceiver.class
package com.rxandroid.receiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import .ConnectivityManager;import workInfo;import .wifi.WifiManager;import android.util.Log;/*** 监听网络状态变化* Created by Travis on /10/11.*/public class NetWorkChangReceiver extends BroadcastReceiver {/*** 获取连接类型** @param type* @return*/private String getConnectionType(int type) {String connType = "";if (type == ConnectivityManager.TYPE_MOBILE) {connType = "3G网络数据";} else if (type == ConnectivityManager.TYPE_WIFI) {connType = "WIFI网络";}return connType;}@Overridepublic void onReceive(Context context, Intent intent) {if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 监听wifi的打开与关闭,与wifi的连接无关int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);Log.e("TAG", "wifiState:" + wifiState);switch (wifiState) {case WifiManager.WIFI_STATE_DISABLED:break;case WifiManager.WIFI_STATE_DISABLING:break;}}// 监听网络连接,包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {//获取联网状态的NetworkInfo对象NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);if (info != null) {//如果当前的网络连接成功并且网络连接可用if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {if (info.getType() == ConnectivityManager.TYPE_WIFI || info.getType() == ConnectivityManager.TYPE_MOBILE) {Log.i("TAG", getConnectionType(info.getType()) + "连上");}} else {Log.i("TAG", getConnectionType(info.getType()) + "断开");}}}}}
(2)基类 BasicActivity
package com.rxandroid.activity;import android.content.IntentFilter;import .ConnectivityManager;import .wifi.WifiManager;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.WorkChangReceiver;/*** Activity基类:实时获取网络状态* create by Travis1022 on -10-11*/public class BasicActivity extends AppCompatActivity {private boolean isRegistered = false;private NetWorkChangReceiver netWorkChangReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//注册网络状态监听广播netWorkChangReceiver = new NetWorkChangReceiver();IntentFilter filter = new IntentFilter();filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);filter.addAction(WORK_STATE_CHANGED_ACTION);filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);registerReceiver(netWorkChangReceiver, filter);isRegistered = true;}@Overrideprotected void onDestroy() {super.onDestroy();//解绑if (isRegistered) {unregisterReceiver(netWorkChangReceiver);}}}
(3) 在AndroidManifest.xml中声明广播以及对应的网络权限
<uses-permission android:name="android.permission.INTERNET"/><!-- 网络状态 --><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><!--监听网络状态--><receiver android:name=".WorkChangReceiver" ><intent-filter><action android:name=".conn.CONNECTIVITY_CHANGE" /><action android:name=".wifi.WIFI_STATE_CHANGED" /><action android:name=".wifi.STATE_CHANGE" /></intent-filter></receiver>
此时就可以实时监听网络状态了。
如果觉得《Android实时监听网络状态》对你有帮助,请点赞、收藏,并留下你的观点哦!