Android gets and determines network connection status

Source: Internet
Author: User

/* Add permissions */
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
/* Add and invoke the following methods where the network needs to be judged */
 Public Booleanisnetworkavilable (Context context) {Connectivitymanager Connectivitymanager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); if(Connectivitymanager = =NULL){            return false; }Else{networkinfo Networkinfo=Connectivitymanager.getactivenetworkinfo (); if(Networkinfo! =NULL ){                                /*determine if the network is connected*/                if(networkinfo.getstate () = =NetworkInfo.State.CONNECTED) {/*determine if the WiFi connection is*/                    if(Networkinfo.gettype () = =Connectivitymanager.type_wifi) {Toast.maketext (Getapplicationcontext (),"WiFi", Toast.length_short). Show (); }                    /*determine if the data connection is*/                    Else if(Networkinfo.gettype () = =connectivitymanager.type_mobile) {Toast.maketext (Getapplicationcontext (),"GPRS", Toast.length_short). Show (); }                    return true; }            }        }        return false; }

If data is connected through Telephonymanager to obtain data is 3g/4g, unicom or mobile telecommunications and other information

/* Get service information from your phone's suppliers * *
Telephonymanager Telephonymanager = (telephonymanager) context.getsystemservice (Context.telephony_service);

Get phone format: int getphonetype ()
int PHONE_TYPE_CDMA mobile phone format for CDMA, telecom
int PHONE_TYPE_GSM mobile phone format for GSM, Mobile and Unicom
int phone_type_none cell phone format unknown

Android gets and determines network connection status

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.