Android determines network type and monitors network status

Source: Internet
Author: User

First, determine whether the network type is WiFi, or 3G, or 2G network

Connectivitymanager connectmgr = (connectivitymanager) this.getsystemservice (Context.connectivity_service);

  networkinfo info = connectmgr.getactivenetworkinfo ();  First, determine whether the network is WiFi, Be sure to make a non-null judgment before judging if there is no network connection Info ==nullinfo.gettype ()  == ConnectivityManager.TYPE_WIFI  II, Determine if the mobile network Info !=null && info.gettype ()  ==  connectivitymanager.type_ mobile  cell phone Network for detailed distinction:  info.getsubtype ()   Here use  getsubtype (), not  gettype (), GetType () returned is 0, Or 1, is the cell phone network or wifi info.getsubtype () value list as follows:           *  NETWORK_TYPE_CDMA  Network type is Cdma         * network_ type_edge  Network type is edge         * network_type_evdo_0  Network type is evdo0         * network_type_evdo_a  network type is EVDOA          * NETWORK_TYPE_GPRS  Network type is gprs          * network_type_hsdpa  Network type is Hsdpa         * network_ type_hspa  Network type is hspa         * network_type_hsupa  Network type is hsupa         * network_type_umts  network type is UMTS

Second, monitoring network status

The following code can monitor whether the network is connected, can do the corresponding operation according to the network connection;

1 Importcom.qdsj.hxcmjj.activity.MainActivity;2 3 ImportAndroid.app.Service;4 ImportAndroid.content.BroadcastReceiver;5 ImportAndroid.content.Context;6 Importandroid.content.Intent;7 ImportAndroid.content.IntentFilter;8 ImportAndroid.net.ConnectivityManager;9 ImportAndroid.net.NetworkInfo;Ten ImportAndroid.os.IBinder; One  A  Public classNetmonitorserviceextendsService { -  -      the      -     PrivateConnectivitymanager Connectivitymanager; -     Privatenetworkinfo info; -  +     PrivateBroadcastreceiver Mreceiver =NewBroadcastreceiver () { -  + @Override A          Public voidOnReceive (Context context, Intent Intent) { atString action =intent.getaction (); -             if(Action.equals (connectivitymanager.connectivity_action)) { -SYSTEM.OUT.PRINTLN ("Network status has changed"); -Connectivitymanager =(Connectivitymanager) Getsystemservice (context.connectivity_service); -info =Connectivitymanager.getactivenetworkinfo ();  -                 if(Info! =NULL&&info.isavailable ()) { inSYSTEM.OUT.PRINTLN ("Network Connection"); -Context.startactivity (NewIntent (context,mainactivity.class). Addflags (Intent.flag_activity_new_task)); to}Else { +SYSTEM.OUT.PRINTLN ("Network Disconnect"); -                 } the             } *         } $     };Panax Notoginseng  -  the @Override +      Publicibinder onbind (Intent Intent) { A         return NULL; the     } +  - @Override $      Public voidonCreate () { $         Super. OnCreate (); -Intentfilter Mfilter =NewIntentfilter (); - mfilter.addaction (connectivitymanager.connectivity_action); the registerreceiver (Mreceiver, mfilter); -System.out.println ("Turn on network monitoring service");Wuyi     } the      -      Wu      - @Override About          Public voidOnDestroy () { $             Super. OnDestroy (); - Unregisterreceiver (mreceiver); -         } -  A @Override +          Public intOnstartcommand (Intent Intent,intFlagsintStartid) { the             return Super. Onstartcommand (Intent, flags, startid); -         } $  the      the      the      the}

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.