Android detects if the network is available and gets the network type

Source: Internet
Author: User

This is used when using Getsystemservice in a class: 1.

 Public classJajamenuextendsActivity { Public StaticJajamenu instance; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Super. Init (); Instance= This; }      //Get Instance       Public StaticJajamenu getinstance () {returninstance; }}

2. Call

 Public Static Final int Nettype_wifi = 0x01;  Public Static Final int Nettype_cmwap = 0x02;  Public Static Final int Nettype_cmnet = 0x03;

/*** Detect if the network is available *@return     */     Public Booleanisnetworkconnected () {Connectivitymanager cm=(Connectivitymanager) jajamenu.getinstance (). Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo=Cm.getactivenetworkinfo (); returnNetworkinfo! =NULL&&networkinfo.isconnectedorconnecting (); }        /*** Get network type *@return0: No network 1:wifi network 2:WAP Network 3:net Network **/     Public intGetnetworktype () {intNettype=0; Connectivitymanager cm=(Connectivitymanager) jajamenu.getinstance (). Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo=Cm.getactivenetworkinfo (); if(Networkinfo = =NULL){            returnNetType; }        intNtype=Networkinfo.gettype (); if(NType = =connectivitymanager.type_mobile) {String ExtraInfo=Networkinfo.getextrainfo (); if(!Extrainfo.isempty ()) {String Strinfo=extrainfo.tolowercase (); if(Strinfo.equals ("Cmnet") ) {NetType=nettype_cmnet; }                Else{NetType=Nettype_cmwap; }            }        }        Else if(NType = =Connectivitymanager.type_wifi) {NetType=Nettype_wifi; }        returnNetType; }

Android detects if the network is available and gets the network type

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.