Package Cn.hackcoder.beautyreader.broadcast;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.net.connectivitymanager;import Android.util.log;import Android.widget.toast;import Cn.hackcoder.beautyreader.activity.base.baseactivity;import Cn.hackcoder.beautyreader.utils.networkutils;public class Networkstatusreceiver extends Broadcastreceiver { Public Networkstatusreceiver () { } @Override public void OnReceive (context context, Intent Intent) { String action = Intent.getaction (); if (Action.equals (connectivitymanager.connectivity_action)) { Toast.maketext (context, "network changed", Toast.length_long). Show (); baseactivity.isnetworkconnected = Networkutils.getapntype (context) >0; }} }
Package Cn.hackcoder.beautyreader.utils;import Android.content.context;import Android.net.ConnectivityManager; Import Android.net.networkinfo;import android.telephony.telephonymanager;/** * Created by Hackcoder on 15-1-25.& Nbsp;*/public class Networkutils { /** * Determine if there is an Internet connection * @param context&nbs P * @return */ public static Boolean isnetworkconnected (context context) {&nbs P if (context! = null) { Connectivitymanager Mconnectivitymana GER = (Connectivitymanager) context Getsystemservice (context.connectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo! = null) { & nbsp return mNeTworkinfo.isavailable (); } } &NBSP ; Return false; } /** * determine if WiFi network is available * @param context * @return */ public static Boolean iswificonnected (context context) { & nbsp if (context! = null) { Connectivitymanager Mconnectivitymanager = ( Connectivitymanager) context Getsystemservice (Conte Xt. Connectivity_service); Networkinfo mwifinetworkinfo = Mconnectivitymanager Getnetworkinfo (Connectivitymanager.type_wifi), & nbsp if (mwifinetworkinfo! = null) { Return mwifinetworkInfo.isavailable (); } } RET Urn false; } /** * determine if mobile network is available * @param context & nbsp * @return */ public static Boolean ismobileconnected (context context) {   ; if (context! = null) { Connectivitymanager Mconnectivitymanager = ( Connectivitymanager) context Getsystemservice (Conte Xt. Connectivity_service); Networkinfo mmobilenetworkinfo = mconnectivitymanager& nbsp Getnetworkinfo (connectivitymanager.type_mobile); & nbsp if (mmobilenetworkinfo! = null) { Retur N MmobileNetworkinfo.isavailable (); } } &NB Sp Return false; } /** * Get the type information for the current network connection * @param context * @return */ public static int Getconnectedtype (context context) { &NB Sp if (context! = null) { Connectivitymanager Mconnectivitymanager = ( Connectivitymanager) context Getsystemservice (Conte Xt. Connectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo! = null & & Mnetworkinfo.isavailable ()) { return Mnetworkinfo.gettype (); } &NBsp } return-1; } /** * get current network status: No network 0:wifi Network 1:3g Network 2:2g network 3 * * @param context * @return */ public static int Getapntype (context context) { int netType = 0; &N Bsp Connectivitymanager connmgr = (connectivitymanager) context Getsystemservice (Context.connectivity_service); Networkinfo networkinfo = Connmgr.getactivenetworkinfo (); if (networkinfo = = null) { &N Bsp Return nettype; } int nType = Networkinfo.gettype (); &N Bsp if (NType = = Connectivitymanager.type_wifi) { NetType = 1;//wifi &n Bsp } else if (NType = = connectivitymanager.type_mobile) { int nsubtype = Networkinf O.getsubtype (); Telephonymanager mtelephony = (telephonymanager) context & nbsp Getsystemservice (context.telephony_service); if (Nsubtype = = telephonymanager.network_type_umts &N Bsp &&!mtelephony.isnetworkroaming ()) { N Ettype = 2;//3g } else { NetType = 3;//2g } } retur n nettype; }}
Registered:
<receiver android:name= ". Broadcast. Networkstatusreceiver " android:enabled=" true " android:exported=" true "> <intent-filter> <action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </ Receiver>
Android Network judgment