Android determines network connection status, networking type, operator

Source: Internet
Author: User

/*** Get access to the Internet * *@paramMcontext *@return     */     Public Staticstring Getnettype (Context mcontext) {String NetType= ""; Connectivitymanager ConnectionManager=(Connectivitymanager) mcontext. Getsystemservice (Context.connectivity_service); Networkinfo Info=Connectionmanager.getactivenetworkinfo (); if(NULL! = Info &&info.isavailable ()) {NetType=Info.gettypename (); }        returnNetType; }    /*** Determine if the network connection is available * *@paramMcontext *@return      */     Public Static BooleanGetnetisvali (Context mcontext) {if(Mcontext! =NULL) {Connectivitymanager Mconnectivitymanager=(Connectivitymanager) mcontext. Getsystemservice (Context.connectivity_service); Networkinfo Mnetworkinfo=Mconnectivitymanager. Getactivenetworkinfo (); if(Mnetworkinfo! =NULL) {                returnmnetworkinfo.isavailable (); }        }        return false; }    /*** Get carrier information * *@paramMcontext *@return     */     Public Staticstring Getnetextrainfo (Context mcontext) {String netextrainfo= ""; Telephonymanager mTm=(Telephonymanager) mcontext. Getsystemservice (Context.telephony_service); if(mtm.getsimstate () = =Telephonymanager.sim_state_ready) {Netextrainfo=Mtm.getsimoperator (); if(NULL!=netextrainfo) {                if(Netextrainfo.equals ("46000")                        || Netextrainfo.equals ("46002")                        || Netextrainfo.equals ("46007")) {                    //China MobileNetextrainfo = "China Mobile"; } Else if(Netextrainfo.equals ("46001")) {                    //China UnicomNetextrainfo = "China Unicom"; } Else if(Netextrainfo.equals ("46003")) {                    //China TelecomNetextrainfo = "China Telecom"; } Else{netextrainfo= "Other"; }            }        }        returnNetextrainfo; }

1. Consider only 1 cards

2. Don't forget to add network permissions

Android determines network connection status, networking type, operator

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.