[Android] Determines if a network connection is available

Source: Internet
Author: User

Tag:android    network connection    

/** * Determine if the mobile network is on * * @param context * @return */public static Boolean isnetenabled (context context) {Telephonymanager TM = (Telephonymanager) context.getsystemservice (Context.telephony_service), if (tm! = null) {if (Tm.getnetworktype ()! = Telephonymanager.network_type_unknown) {log.i ("SJF", "mobile network is turned on"); return true;}} LOG.I ("SJF", "mobile network not yet turned on"); return false;} /** * Determine if WiFi network is on * * @param context * @return */public static Boolean iswifienabled (context context) {Wifimanager wm = (Wifimanager) Context.getsystemservice (Context.wifi_service), if (wm! = null && wm.iswifienabled ()) {LOG.I (" SJF "," WiFi network is turned on "); return true; LOG.I ("SJF", "WiFi network not turned on"); return false;} /** * Determine if the mobile network is connected successfully * * @param context * @return */public static Boolean isnetcontected (context context) {Connectivitymana Ger cm = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo info = cm.getnetworkinfo (connectivitymanager.type_mobile); if (cm! = NULL && info! = NULL && in Fo. IsConnected ()) {log.i ("SJF", "Mobile network Connection succeeded"); return true; LOG.I ("SJF", "Mobile network Connection Failed"); return false;} /** * Determine if WiFi connection is successful * * @param context * @return */public static Boolean iswificontected (context context) {Connectivityman Ager cm = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo info = cm.getnetworkinfo (Connectivitymanager.type_wifi), if (info! = null && info.isconnected ()) { LOG.I ("SJF", "WiFi network connection succeeded"); return true; LOG.I ("SJF", "WiFi network Connection Failed"); return false;} /** * Determine if mobile network and WiFi is on * * @param context * @return */public static Boolean isnetworkenabled (context context) {return (IsN Etenabled (context) | | Iswifienabled (context));} /** * Determine if the mobile network and WiFi are connected successfully * @param context * @return */public static Boolean isnetworkconnected (context context) {return (IS Wificontected (context) | | Isnetcontected (context));}

[Android] Determines if a network connection is available

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.