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 Pub
LIC void OnReceive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals (connectivitymanager.connectivity_action)) {Toast.maketext (context, "network changed", Toast.lengt
H_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. */public class Networkutils { /** * Determine if there is a network connection * @param cont Ext * @return */ public static Boolean isnetworkconnected (Context Co ntext) { if (context!= null) { Connectivitymanager Mconnectivitymanager = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo!= null) { &NBSP
return mnetworkinfo.isavailable ();
{ } return false; } /** * determine if WiFi is available * @param context * @return */ public static Boolean iswificonnected (context context) { &NBSP ; IF (context!= null) { Connectivitymanager Mconnectivitymanager = (Connectivitymanager) Context Getsystemservice (Cont Ext.
Connectivity_service); Networkinfo mwifinetworkinfo = mconnectivitymanager
. Getnetworkinfo (Connectivitymanager.type_wifi); if (mwifinetworkinfo!= null) {  
return mwifinetworkinfo.isavailable ();
{ } return false; } /** * determine if mobile network is available * @param context &NBSP ; * @return */ public static Boolean ismobileconnected (context context) { &NB Sp IF (context!= null) { Connectivitymanager Mconnectivitymanager = (Connectivitymanager) Context Getsystemservice (Cont Ext.
Connectivity_service); Networkinfo mmobilenetworkinfo = mconnectivitymanager &NBSP ;
. Getnetworkinfo (Connectivitymanager.type_mobile); if (mmobilenetworkinfo!= null) { 
return mmobilenetworkinfo.isavailable ();
{ } return false; } /** * get current network connection type information * @param context * @return */ public static int Getconnectedtype (context context) { IF (context!= null) { Connectivitymanager Mconnectivitymanager = (Connectivitymanager) Context Getsystemservice (Cont Ext.
Connectivity_service);
Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo!= null && mnetworkinfo.isavailable ()) { return Mnetworkinfo.gettype ();
{ return-1; } /** * get current network status: No network 0:wifi network 1:3g Network 2:2g Network 3 *   ; * @param * @return */ public static int GETAPNT Ype (Context context) { int nettype = 0; Connectivitymanager CONNMG R = (Connectivitymanager) context Getsystemservice (Context.connec
Tivity_service);
Networkinfo networkinfo = Connmgr.getactivenetworkinfo (); if (Networkinfo = null) { return NetType &N Bsp
} int ntype = Networkinfo.gettype (); if (ntype = ConnectivitymanAger. Type_wifi) { NetType = 1;//WIFI } else if (ntype = = Connectivitymanager.type_mobile) { int nsubtype = Networkinfo.getsubtype (); &nb Sp Telephonymanager mtelephony = (telephonymanager) context &N Bsp
. Getsystemservice (Context.telephony_service); if (Nsubtype = telephonymanager.network_type_umts & nbsp &&!mtelephony.isnetworkroaming ()) { NetType = 2;//3G } else { &NBS P NetType = 3;//2G } &N Bsp
return 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>