Thinkandroid Framework Network State Monitoring principle

Source: Internet
Author: User

Thinkandroid Frame Network State monitoring principle, the need for friends can refer to the next.


Now the use of the network to listen to more and more places, here to provide the next thinkandroid in the implementation method, or according to the functional principle of the inverted code:

1, the network status will be broadcast, listening network status must be achieved through the broadcast receiver, broadcast recipients:

/** * @Title networkstatereceiver * @Package com.ta.util.netstate * @Description is a detection network status change, need to configure <receiver *Android: Name= "Com.ta.util.netstate.TANetworkStateReceiver" > * <intent-filter> <action * An Droid:name= "Android.net.conn.CONNECTIVITY_CHANGE"/> <action * android:name= "Android.gzcpc.conn.CONNECT               Ivity_change "/> * </intent-filter> </receiver> * * requires permission to open <uses-permission * Android:name= "Android.permission.CHANGE_NETWORK_STATE"/> * <uses-permission *Android: Name= "Android.permission.CHANGE_WIFI_STATE"/> * <uses-permission * android:name= "Android . permission. Access_network_state "/> * <uses-permission * android:name=" Android.permission.ACCESS_WIFI _state "/> * @author White cat * @date 2013-5-5 PM 22:47 * @version V1.2 */public class Tanetworkstatereceiver extends BROADCA Streceiver{private static Boolean networkavailable = false;private static NetType nettype;private static arraylist< tanetchangeobserver> netchangeobserverarraylist = new arraylist<tanetchangeobserver> ();p rivate final static String android_net_change_action = "Android. Net.conn.CONNECTIVITY_CHANGE ";p ublic final static String ta_android_net_change_action =" Ta.android.net.conn.CONNECTIVITY_CHANGE ";p rivate static broadcastreceiver receiver;private static Broadcastreceiver Getreceiver () {if (receiver = = NULL) {receiver = new tanetworkstatereceiver ();} return receiver;} @Overridepublic void OnReceive (context context, Intent Intent) {receiver = Tanetworkstatereceiver.this;if ( Intent.getaction (). Equalsignorecase (android_net_change_action) | | Intent.getaction (). Equalsignorecase (ta_android_net_change_action)) {talogger.i (Tanetworkstatereceiver.this, " Network status change. "); if (! Tanetworkutil.isnetworkavailable (context)) {talogger.i (tanetworkstatereceiver.this, "No network connection."); Networkavailable = false;} ELSE{TALOGGER.I (tanetworkstatereceiver.this, "network connection succeeded."); NetType = Tanetworkutil.getapntype (context); networkavailable = true;} Notifyobserver ();}} /** * Register Network status Broadcast * * @param mcontext */public static void Registernetworkstatereceiver (Context mcontext) {Intentfilter Filte r = New IntentfilteR (); filter.addaction (ta_android_net_change_action); filter.addaction (android_net_change_action); Mcontext.getapplicationcontext (). Registerreceiver (Getreceiver (), filter);} /** * Check network status * * @param mcontext */public static void Checknetworkstate (Context mcontext) {Intent Intent = new Intent (); in Tent.setaction (ta_android_net_change_action); Mcontext.sendbroadcast (intent);} /** * Logoff network status Broadcast * * @param mcontext */public static void Unregisternetworkstatereceiver (Context mcontext) {if (Receiver! = NULL) {Try{mcontext.getapplicationcontext (). Unregisterreceiver (receiver),} catch (Exception e) {//Todo:handle EXCEPTIONTALOGGER.D ("Tanetworkstatereceiver", E.getmessage ());}}} /** * Gets the current network status, true for network connection succeeded, otherwise network connection failed * * @return */public static Boolean isnetworkavailable () {return networkavailable;} public static NetType Getapntype () {return netType;}  private void Notifyobserver () {for (int i = 0; i < netchangeobserverarraylist.size (); i++) {Tanetchangeobserver Observer = Netchangeobserverarraylist.get (i); if (obsErver! = null) {if (isnetworkavailable ()) {observer.onconnect (netType);} else{observer.ondisconnect ();}}}  /** * Registered Network Connection Viewer * * @param observerkey * observerkey */public static void Registerobserver (Tanetchangeobserver observer) {if (netchangeobserverarraylist = = null) {netchangeobserverarraylist = new Arraylist<tanetchangeobserver > ();} NETCHANGEOBSERVERARRAYLIST.ADD (Observer);}  /** * Log Off Network connection Viewer * * @param resID * observerkey */public static void Removeregisterobserver (Tanetchangeobserver observer) {if (netchangeobserverarraylist! = null) {NETCHANGEOBSERVERARRAYLIST.REMOVE (Observer);}}}


2, after the broadcast recipient receives the broadcast, it is through the set of observers, passing back the message, the Observer:

/** * @Title tanetchangeobserver * @Package com.ta.util.netstate * @Description is detection  network   changed observers * @author White cat * @date  2013-1-22 pm 9:35 * @version V1.0 */public class tanetchangeobserver{/** * network connection when calling */public void OnConnect (NetType type  ) {}/** * There is currently no network connection */ public void OnDisconnect () {}} 

3, the Observer, is a listening interface. The tool class used in the broadcast receiver:

/** * @Title networkutil * @Package com.ta.util.netstate * @Description is detectionNetworkA toolkit * @author White cat * @date 2013-1-22 PM 9:35 * @version V1.0 */public class Tanetworkutil{public static enum Nettype{wifi , CMNET, Cmwap, nonenet}/** * Network Available * * @param context * @return */public static Boolean isnetworkavailable (Context cont EXT) {Connectivitymanager mgr = (connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo[] info = mgr.getallnetworkinfo (); if (info! = null) {for (int i = 0; i < info.length; i++) {if (Info[i].getsta Te () = = NetworkInfo.State.CONNECTED) {return true;}}} return false;} /** * Determine if there isNetworkConnection * * @param context * @return */public static Boolean isnetworkconnected (context context) {if (context! = null) {Connecti Vitymanager Mconnectivitymanager = (connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo! = null) {return Mnetworkinfo.isavailable ();}} return false;} /** * Judging WiFiNetworkAvailable * * @param context * @return */public static Boolean iswificonnected (context context) {if (context! = null) {Connectiv Itymanager Mconnectivitymanager = (connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mwifinetworkinfo = Mconnectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); Mwifinetworkinfo! = null) {return mwifinetworkinfo.isavailable ();}} return false;} /** * Judging MobileNetworkAvailable * * @param context * @return */public static Boolean ismobileconnected (context context) {if (context! = null) {Connect Ivitymanager Mconnectivitymanager = (connectivitymanager) context.getsystemservice (Context.CONNECTIVITY_SERVICE); Networkinfo mmobilenetworkinfo = Mconnectivitymanager.getnetworkinfo (connectivitymanager.type_mobile); Mmobilenetworkinfo! = null) {return mmobilenetworkinfo.isavailable ();}} return false;} /** * Get currentNetworkType information for the connection * * @param context * @return */public static int Getconnectedtype (context context) {if (context! = null) {Connectiv Itymanager Mconnectivitymanager = (connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo! = null && Mnetworkinfo.isavailable ()) {return Mnetworkinfo.gettype ();}} return-1;} /** * * @author White cat * * Get currentNetworkStatus-1: No network 1:wifi network 2:WAP Network 3:net Network * * @param context * * @return */public static NetType Getapntype (context context) {Co Nnectivitymanager connmgr = (connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo networkinfo = Connmgr.getactivenetworkinfo (); if (networkinfo = = null) {return nettype.nonenet;} int nType = Networkinfo.gettype (); if (NType = = Connectivitymanager.type_mobile) {if (Networkinfo.getextrainfo (). toLowerCase (). Equals ("Cmnet")) {return nettype.cmnet;} Else{return Nettype.cmwap;}} else if (NType = = Connectivitymanager.type_wifi) {return nettype.wifi;} return nettype.nonenet;}}


Well, the network listener uses these three classes to see how thinkandroid is used:

Since the network listener is effective for all pages, set a global observer for the broadcast receiver when application is created:

Tanetchangeobserver = new Tanetchangeobserver () {@Overridepublic void OnConnect (NetType type) {//TODO auto-generated Method Stubsuper.onconnect (type); TAApplication.this.onConnect (type);} @Overridepublic void OnDisconnect () {//TODO auto-generated method Stubsuper.ondisconnect (); TAApplication.this.onDisConnect ();}}; Tanetworkstatereceiver.registerobserver (Tanetchangeobserver);
Network type) {//TODO auto-generated Method stubnetworkavailable = true;if (currentactivity! = null) {Currentactivity.onconnect (type) ;}}

Thinkandroid Framework Network State Monitoring principle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.