ThinkAndroid framework network status monitoring principle, thinkandroid framework

Source: Internet
Author: User

ThinkAndroid framework network status monitoring principle, thinkandroid framework

The Network Status Monitoring principle of the ThinkAndroid framework. If you need it, refer to it.


Nowadays, network listening is more and more widely used. The Implementation Method in ThinkAndroid is provided here, or the code is displayed based on the functional principle:

1. The network status is broadcast. The Listener network status must be implemented by the broadcast receiver. The broadcast receiver:

/*** @ Title NetworkStateReceiver * @ Package com. ta. util. netstate * @ Description is a change in network status. You need to configure <er * android: name = "com. ta. util. netstate. TANetworkStateReceiver "> * <intent-filter> <action * android: name =" android.net. conn. CONNECTIVITY_CHANGE "/> <action * android: name =" android.gz cpc. conn. CONNECTIVITY_CHANGE "/> * </intent-filter> </Cycler> ** permission <uses-permission * android: name =" android Oid. 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 * @ version V1.2 */public class TANetworkStateReceiver extends BroadcastReceiver {private st Atic Boolean networkAvailable = false; private static netType; private static ArrayList <TANetChangeObserver> netChangeObserverArrayList = new ArrayList <TANetChangeObserver> (); private final static String login = "android.net. conn. CONNECTIVITY_CHANGE "; public final static String TA_ANDROID_NET_CHANGE_ACTION =" ta.android.net. conn. CONNECTIVITY_CHANGE "; private static Broadcast Extends er extends er; private static BroadcastReceiver getReceiver () {if (Receiver ER = null) {receiver ER = new TANetworkStateReceiver ();} return receiver er ;}@ 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 (TANetw OrkStateReceiver. this, "network status changed."); if (! TANetWorkUtil. isNetworkAvailable (context) {TALogger. I (TANetworkStateReceiver. this, "No network connection. "); networkAvailable = false;} else {TALogger. I (TANetworkStateReceiver. this, "network connection successful. "); netType = TANetWorkUtil. getAPNType (context); networkAvailable = true;} yyobserver () ;}/ *** register the network status broadcast ** @ param mContext */public static void registerNetworkStateReceiver (Context mContext) {IntentFilter filter = new IntentF Ilter (); filter. addAction (TA_ANDROID_NET_CHANGE_ACTION); filter. addAction (ANDROID_NET_CHANGE_ACTION); mContext. getApplicationContext (). registerReceiver (getReceiver (), filter);}/*** check the network status ** @ param mContext */public static void checkNetworkState (Context mContext) {Intent intent = new Intent (); intent. setAction (TA_ANDROID_NET_CHANGE_ACTION); mContext. sendBroadcast (intent);}/*** cancel network status broadcast ** @ param mCo Ntext */public static void unRegisterNetworkStateReceiver (Context mContext) {if (receiver! = Null) {try {mContext. getApplicationContext (). unregisterReceiver (handler ER);} catch (Exception e) {// TODO: handle exceptionTALogger. d ("TANetworkStateReceiver", e. getMessage () ;}}/ *** get the current network status. true indicates that the network connection is successful. Otherwise, the network connection fails. ** @ return */public static Boolean isNetworkAvailable () {return networkAvailable;} public static netType getAPNType () {return netType;} private void policyobserver () {for (int I = 0; I <netCh AngeObserverArrayList. size (); I ++) {TANetChangeObserver observer = netChangeObserverArrayList. get (I); if (observer! = Null) {if (isNetworkAvailable () {observer. onConnect (netType);} else {observer. onDisConnect () ;}}}/*** register the network connection observer ** @ param observerKey */public static void registerObserver (TANetChangeObserver observer) {if (netChangeObserverArrayList = null) {netChangeObserverArrayList = new ArrayList <TANetChangeObserver> ();} netChangeObserverArrayList. add (observer);}/*** disconnect the network access observer ** @ param resID * ObserverKey */public static void removeRegisterObserver (TANetChangeObserver observer) {if (netChangeObserverArrayList! = Null) {netChangeObserverArrayList. remove (observer );}}}


2. After receiving the broadcast, the broadcast receiver transmits the information back to the set observer. The observer:

/*** @ Title TANetChangeObserver * @ Package com. ta. util. netstate * @ Description is the observer for network change detection * @ author white cat * @ date 2013-1-22 * @ version V1.0 */public class TANetChangeObserver {/*** called during network connection * /public void onConnect (netType type) {}/ *** no network connection currently */public void onDisConnect (){}}

3. The observer is a listener interface. Tool classes used in broadcast receivers:

/*** @ Title NetWorkUtil * @ Package com. ta. util. netstate * @ Description is a network Tool Kit * @ author white cat * @ date 2013-1-22 * @ version V1.0 */public class TANetWorkUtil {public static enum netType {wifi, CMNET, CMWAP, noneNet}/*** network availability ** @ param context * @ return */public static boolean isNetworkAvailable (Context context) {ConnectivityManager mgr = (ConnectivityManager) context. getSystemService (Con Text. CONNECTIVITY_SERVICE); NetworkInfo [] info = mgr. getAllNetworkInfo (); if (info! = Null) {for (int I = 0; I <info. length; I ++) {if (info [I]. getState () = NetworkInfo. state. CONNECTED) {return true ;}} return false;}/*** determine whether a network connection exists ** @ param context * @ return */public static boolean isNetworkConnected (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager. getActiveNetworkInfo (); if (mNetworkInfo! = Null) {return mNetworkInfo. isAvailable () ;}} return false;}/*** determine whether the Wi-Fi network is available ** @ param context * @ return */public static boolean isWifiConnected (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networking = mConnectivityManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI); if (mWiFiNetworkInfo! = Null) {return mWiFiNetworkInfo. isAvailable () ;}} return false;}/*** determine whether the MOBILE network is available ** @ param context * @ return */public static boolean isMobileConnected (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networking = mConnectivityManager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE); if (mMobileNetworkInfo! = Null) {return mMobileNetworkInfo. isAvailable () ;}} return false;}/*** obtain the type information of the current network connection ** @ param context * @ return */public static int getConnectedType (Context context) {if (context! = Null) {ConnectivityManager 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 the current network status-1: No network 1: WIFI network 2: wap network 3: net network ** @ param context ** @ return */public static netType getAPNType (Context context) {ConnectivityManager 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, network listening uses these three classes to see how ThinkAndroid is used:

Because network listening is effective for all pages, you can set a global observer for the broadcast receiver when creating the Application:

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);
/*** No network connection currently */public void onDisConnect () {networkAvailable = false; if (currentActivity! = Null) {currentActivity. onDisConnect () ;}}/*** called during network connection */protected void onConnect (netType type) {// TODO Auto-generated method stubnetworkAvailable = true; if (currentActivity! = Null) {currentActivity. onConnect (type );}}

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.