Android 开发工具类 19_NetworkStateReceiver
检测网络状态改变类:
1、注册网络状态广播;
2、检查网络状态;
3、注销网络状态广播;
4、获取当前网络状态,true为网络连接成功,否则网络连接失败;
5、注册网络连接观察者;
6、注销网络连接观察者。
1 import java.util.ArrayList; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.IntentFilter; 7 import android.util.Log; 8 9 import com.example.androidnetworkdemo.observer.NetChangeObserver; 10 import com.example.androidnetworkdemo.utils.NetWorkUtil; 11 import com.example.androidnetworkdemo.utils.NetWorkUtil.NetType; 12 13 /** 14 * 是一个检测网络状态改变的,需要配置 15 * <receiver android:name="com.osrad.common.receiver.OSNetworkStateReceiver" > 16 * <intent-filter> 17 * <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 18 * <action android:name="os.android.net.conn.CONNECTIVITY_CHANGE" /> 19 * </intent-filter> 20 * </receiver> 21 * 需要开启权限 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 22 * <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 23 * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 24 * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 25 */ 26 public class NetworkStateReceiver extends BroadcastReceiver { 27 28 private static Boolean networkAvailable = false; 29 private static NetType netType; 30 private static ArrayList<NetChangeObserver> netChangeObserverArrayList = new ArrayList<NetChangeObserver>(); 31 private final static String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; 32 public final static String OS_ANDROID_NET_CHANGE_ACTION = "os.android.net.conn.CONNECTIVITY_CHANGE"; 33 private static BroadcastReceiver receiver; 34 35 private static BroadcastReceiver getReceiver() { 36 if (receiver == null) { 37 receiver = new NetworkStateReceiver(); 38 } 39 return receiver; 40 } 41 42 @Override 43 public void onReceive(Context context, Intent intent) { 44 receiver = NetworkStateReceiver.this; 45 if (intent.getAction().equalsIgnoreCase(ANDROID_NET_CHANGE_ACTION) || intent.getAction().equalsIgnoreCase(OS_ANDROID_NET_CHANGE_ACTION)) { 46 if (!NetWorkUtil.isNetworkAvailable(context)) { 47 networkAvailable = false; 48 } else { 49 netType = NetWorkUtil.getAPNType(context); 50 networkAvailable = true; 51 } 52 notifyObserver(); 53 } 54 } 55 56 /** 57 * 注册网络状态广播 58 * 59 * @param mContext 60 */ 61 public static void registerNetworkStateReceiver(Context mContext) { 62 63 IntentFilter filter = new IntentFilter(); 64 filter.addAction(OS_ANDROID_NET_CHANGE_ACTION); 65 filter.addAction(ANDROID_NET_CHANGE_ACTION); 66 mContext.getApplicationContext().registerReceiver(getReceiver(), filter); 67 } 68 69 /** 70 * 检查网络状态 71 * 72 * @param mContext 73 */ 74 public static void checkNetworkState(Context mContext) { 75 76 Intent intent = new Intent(); 77 intent.setAction(OS_ANDROID_NET_CHANGE_ACTION); 78 mContext.sendBroadcast(intent); 79 } 80 81 /** 82 * 注销网络状态广播 83 * 84 * @param mContext 85 */ 86 public static void unRegisterNetworkStateReceiver(Context mContext) { 87 if (receiver != null) { 88 try { 89 mContext.getApplicationContext().unregisterReceiver(receiver); 90 } catch (Exception e) { 91 Log.d(NetworkStateReceiver.class.getName(), e.getMessage()); 92 } 93 } 94 95 } 96 97 /** 98 * 获取当前网络状态,true为网络连接成功,否则网络连接失败 99 * 100 * @return 101 */ 102 public static Boolean isNetworkAvailable() { 103 return networkAvailable; 104 } 105 106 public static NetType getAPNType() { 107 return netType; 108 } 109 110 private void notifyObserver() { 111 112 for (int i = 0; i < netChangeObserverArrayList.size(); i++) { 113 NetChangeObserver observer = netChangeObserverArrayList.get(i); 114 if (observer != null) { 115 if (isNetworkAvailable()) { 116 observer.onNetConnect(netType); 117 } else { 118 observer.onNetDisconnect(); 119 } 120 } 121 } 122 } 123 124 /** 125 * 注册网络连接观察者 126 * 127 * @param observer 观察者实例 128 */ 129 public static void registerObserver(NetChangeObserver observer) { 130 if (netChangeObserverArrayList == null) { 131 netChangeObserverArrayList = new ArrayList<NetChangeObserver>(); 132 } 133 netChangeObserverArrayList.add(observer); 134 } 135 136 /** 137 * 注销网络连接观察者 138 * 139 * @param observer 观察者实例 140 */ 141 public static void removeRegisterObserver(NetChangeObserver observer) { 142 if (netChangeObserverArrayList != null) { 143 netChangeObserverArrayList.remove(observer); 144 } 145 } 146 147 }
检测网络改变的观察者
1 import com.example.androidnetworkdemo.utils.NetWorkUtil.NetType; 2 3 /** 4 * 是检测网络改变的观察者 5 */ 6 public interface NetChangeObserver 7 { 8 /** 9 * 网络连接连接时调用 10 */ 11 public void onNetConnect(NetType type); 12 13 /** 14 * 当前没有网络连接 15 */ 16 public void onNetDisconnect(); 17 }
调用示例
1 public class BaseApplication extends Application { 2 3 private NetChangeObserver changeObserver = null; 4 5 public static Activity currentActivity; 6 7 @Override 8 public void onCreate() { 9 super.onCreate(); 10 // 做初始化操作 11 doOnCreate(); 12 } 13 14 /** 15 * 此方法做初始化操作 16 */ 17 private void doOnCreate() { 18 // 声明observer注册到网络状态广播接收器中 19 changeObserver = new NetChangeObserver() { 20 @Override 21 public void onNetConnect(NetType type) { 22 BaseApplication.this.onNetConnect(type); 23 } 24 25 @Override 26 public void onNetDisconnect() { 27 BaseApplication.this.onNetDisconnect(); 28 } 29 }; 30 // 注册网络状态监听对象 31 NetworkStateReceiver.registerObserver(changeObserver); 32 } 33 34 /** 35 * 当前没有网络连接 36 */ 37 public void onNetDisconnect() { 38 if (currentActivity != null && currentActivity instanceof BaseActivity) { 39 ((BaseActivity)currentActivity).onNetDisconnect(); 40 } 41 } 42 43 /** 44 * 网络连接连接时调用 45 */ 46 protected void onNetConnect(NetType type) { 47 if (currentActivity != null && currentActivity instanceof BaseActivity) { 48 ((BaseActivity)currentActivity).onNetConnect(type); 49 } 50 } 51 52 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。