android-determine if the current network is available

Source: Internet
Author: User

1. Claim Permissions <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

2. Network Detection class

 Public classNetworkdetector {/**Network type is unknown*/     Public Static Final intNetwork_type_unknown = 0; /**Current network is GPRS*/     Public Static Final intNetwork_type_gprs = 1; /**Current network is EDGE*/     Public Static Final intNetwork_type_edge = 2; /**Current network is UMTS*/     Public Static Final intNETWORK_TYPE_UMTS = 3; /**Current Network is Cdma:either is95a or is95b*/     Public Static Final intNETWORK_TYPE_CDMA = 4; /**Current Network is EVDO revision 0*/     Public Static Final intNetwork_type_evdo_0 = 5; /**Current Network is EVDO revision A*/     Public Static Final intNetwork_type_evdo_a = 6; /**Current network is 1xRTT*/     Public Static Final intNetwork_type_1xrtt = 7; /**Current network is HSDPA*/     Public Static Final intNETWORK_TYPE_HSDPA = 8; /**Current network is HSUPA*/     Public Static Final intNetwork_type_hsupa = 9; /**Current network is HSPA*/     Public Static Final intNetwork_type_hspa = 10; /**Current network is IDen*/     Public Static Final intNetwork_type_iden = 11; /**Current Network is EVDO revision B*/     Public Static Final intNetwork_type_evdo_b = 12; /**Current network is LTE*/     Public Static Final intNetwork_type_lte = 13; /**Current network is EHRPD*/     Public Static Final intNETWORK_TYPE_EHRPD = 14; /**Current network is hspa+*/     Public Static Final intNETWORK_TYPE_HSPAP = 15; /**Unknown Network class. {@hide}*/     Public Static Final intNetwork_class_unknown = 0; /**Class of broadly defined "2G" networks. {@hide}*/     Public Static Final intNetwork_class_2_g = 1; /**Class of broadly defined "3G" networks. {@hide}*/     Public Static Final intNetwork_class_3_g = 2; /**Class of broadly defined "4G" networks. {@hide}*/     Public Static Final intNetwork_class_4_g = 3; /**Class of broadly defined "WIFI" networks. {@hide}*/     Public Static Final intNetwork_class_wifi = 4; /**Class of broadly defined "nonet" networks. {@hide}*/     Public Static Final intNetwork_class_nonet = 5; /*** Determine if the current network is available * *@paramContext *@returntrue: Available, false: unavailable;*/     Public Static Booleanisavailable (Context context) {Connectivitymanager Manager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(Manager = =NULL) {            return false; } networkinfo Networkinfo=Manager.getactivenetworkinfo (); //networkinfo.isavailable () returns False if the network is available to return true.         if(Networkinfo = =NULL|| !networkinfo.isavailable ()) {            return false; } Else {            return true; }    }    /*** Determine the current network status * *@paramContext *@return0, unknown, 1, 2G, 2, 3G, 3, 4G, 4, WiFi, 5, nonet;*/     Public Static intGetnetworktype (Context context) {Connectivitymanager Manager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(Manager = =NULL) {            returnnetwork_class_nonet; } networkinfo Networkinfo=Manager.getactivenetworkinfo (); if(Networkinfo = =NULL|| !networkinfo.isavailable ()) {            returnnetwork_class_nonet; }        if(Networkinfo.gettype () = =Connectivitymanager.type_wifi) {            returnNetwork_class_wifi; } Else {            returnGetnetworkclass ((telephonymanager) context. Getsystemservice (Context.telephony_service))        . Getnetworktype ()); }    }     Public Static intGetnetworkclass (intNetworktype) {        Switch(networktype) { CaseNetwork_type_gprs: CaseNetwork_type_edge: CaseNETWORK_TYPE_CDMA: CaseNetwork_type_1xrtt: CaseNetwork_type_iden:returnNetwork_class_2_g;  CaseNetwork_type_umts: Casenetwork_type_evdo_0: Casenetwork_type_evdo_a: CaseNETWORK_TYPE_HSDPA: CaseNetwork_type_hsupa: CaseNetwork_type_hspa: CaseNetwork_type_evdo_b: CaseNETWORK_TYPE_EHRPD: CaseNETWORK_TYPE_HSPAP:returnNetwork_class_3_g;  CaseNetwork_type_lte:returnNetwork_class_4_g; default:            returnNetwork_class_unknown; }    }}

android-determine if the current network is available

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.