Android Current network connection type judging

Source: Internet
Author: User

 Packagenet.nyist.netState; ImportAndroid.content.Context; ImportAndroid.net.ConnectivityManager; ImportAndroid.net.NetworkInfo; /**@author* This is a tool class to judge the current network status **/   Public classNetstate {/**      *       * @returnwhether there is an active network connection*/       Public Final Booleanhasnetworkconnection (Context context) {//Get connection Activity manager        FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context.          Getsystemservice (Context.connectivity_service); //Get link Network information        FinalNetworkinfo networkinfo=Connectivitymanager.getactivenetworkinfo (); return(networkinfo!=NULL&&networkinfo.isavailable ()); }      /**      * @returnReturns a Boolean, whether it is a WiFi network **/       Public Final Booleanhaswificonnection (Context context) {FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context.          Getsystemservice (Context.connectivity_service); FinalNetworkinfo networkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); //whether there is a network and is already connected        return(networkinfo!=NULL&&networkinfo.isconnectedorconnecting ()); }            /**      * @returnreturns a Boolean that determines whether the network is available, whether it is a mobile network **/             Public Final Booleanhasgprsconnection (Context context) {//Get active Connection Manager        FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context.          Getsystemservice (Context.connectivity_service); FinalNetworkinfo networkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_mobile); return(networkinfo!=NULL&&networkinfo.isavailable ()); }      /**      * @returndetermine if the network is available and return the network type, Connectivitymanager.type_wifi,connectivitymanager.type_mobile, unavailable return-1*/       Public Static Final intGetnetworkconnectiontype (Context context) {FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context.          Getsystemservice (Context.connectivity_service); FinalNetworkinfo wifinetworkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); FinalNetworkinfo mobilenetworkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_mobile); if(wifinetworkinfo!=NULL&&wifinetworkinfo.isavailable ()) {              returnConnectivitymanager.type_wifi; }          Else if(mobilenetworkinfo!=NULL&&mobilenetworkinfo.isavailable ()) {              returnConnectivitymanager.type_mobile; }          Else {              return-1; }                          }        }

Android Current network connection type judging

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.