Android Get network connection information

Source: Internet
Author: User




Tool class
/*** Get Network connection Information * * According to Networkinfo can know there are a lot of connection methods and information * *① when there is no available network, Networkinfo is null to determine whether Networkinfo is null* *② When only WiFi network or WiFi network and mobile network exist simultaneously, return WiFi network connection information * * Networkinfo parameter is as follows: * * detailedstate:connected (Connection status) * * ExtraInfo: YITENG1 (WiFi Network Name) * * ISAVAIABLE:TRUE (available) * * networktype:1* * state:connected* * subtype:0* * TYPENAME:WIFI (network type name) * *③ When only the mobile network, return the mobile network connection information * * Networkinfo parameters are as follows: * * detailedstate:connected (Connection status) * * extrainfo:cmnet (mobile network Name) * * Isavaiable: true* * networktype:0* * state:connected* * subtype:2* * typename:mobile (network type name) * *@paramcontext*@return*/ Public StaticNetworkinfo getactivenetwork (context context) {if(Context = =NULL) {return NULL;} Connectivitymanager Mconnmgr=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); if(Mconnmgr = =NULL) {return NULL;} //Get Active network connection informationNetworkinfo Aactiveinfo=Mconnmgr.getactivenetworkinfo (); returnAactiveinfo;}




How to use: on a page that needs to listen to the network

Networkinfo Networkinfo = Getsysteminfo.getactivenetwork ( This); if(Networkinfo = =NULL) {Toastutils.showcustomtoastlong ("No network connection available, check your network settings"); } Else {        if(networkinfo.isavailable () = =false) {Toastutils.showcustomtoastlong ("No network connection available, check your network settings"); } Else{String Networktype=Networkinfo.gettypename (); if(AppConstants.NETWORK_WIFI.equalsIgnoreCase (Networktype)) {Toastutils.showcustomtoastlong ("Connected to WiFi network" +networkinfo.getextrainfo ()); } Else if(AppConstants.NETWORK_MOBILE.equalsIgnoreCase (Networktype)) {Toastutils.showcustomtoastlong ("You are currently using a mobile network"); }            }        }

Android Get network connection information

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.