Android determines whether a network is available and whether WiFi is on

Source: Internet
Author: User

1. Determine whether a network is available

Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> connectivitymanager mconnectivitymanager = (connectivitymanager) This. getsystemservice (context. connectivity_service); <br/> networkinfo mnetworkinfo = mconnectivitymanager. getactivenetworkinfo (); <br/> If (mnetworkinfo. isavailable () <br/> system. out. println ("***** there is available net work"); <br/> else <br/> system. out. println ("***** there is no available net work"); <br/> // obtain whether the specified network type is available, type_movile and type_wifi <br/> mnetworkinfo = mconnectivitymanager. getnetworkinfo (connectivitymanager. type_mobile); <br/> system. out. println ("**** net work type =" + mnetworkinfo. getType (); </P> <p> system. out. println ("*********** extra info =" + mnetworkinfo. getextrainfo (); </P> <p>}

 

2. Determine whether WiFi is on

Public static Boolean checkwifi (activity activitiy) {<br/> wifimanager mwifimanager = (wifimanager) activitiy <br/>. getsystemservice (context. wifi_service); <br/> wifiinfo = mwifimanager. getconnectioninfo (); <br/> int IPaddress = wifiinfo = NULL? 0: wifiinfo. getipaddress (); <br/> If (mwifimanager. iswifienabled () & IPaddress! = 0) {<br/> system. out. println ("*** WiFi is on"); <br/> return true; <br/>}else {<br/> system. out. println ("**** WiFi is off"); <br/> return false; <br/>}< br/>}

 

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.