Android detects whether a network exists

Source: Internet
Author: User

It is mainly used to check whether there is a network. If not, set the network in wifi...

The following is the main code:

Private void checkNetWorkInfo () {if (! Tools. isNetworkAvailable (this) {new AlertDialog. Builder (this). setTitle ("prompt! "). SetIcon (android. r. drawable. ic_dialog_info ). setMessage ("You have not enabled the network, Please enable it "). setNegativeButton ("cancel", null ). setPositiveButton ("enabled", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {startActivity (new Intent (Settings. ACTION_WIRELESS_SETTINGS); // enter the wireless network configuration page startActivity (new Intent (Settings. ACTION_WIFI_SETTINGS); // enter the wifi network settings page on your phone }}). show ();}}

Code in isNetworkAvailable:

/** Determine whether a network exists */public static boolean isNetworkAvailable (Context context) {ConnectivityManager cm = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); if (cm = null) {return false;} else {// print all network statuses NetworkInfo [] infos = cm. getAllNetworkInfo (); if (infos! = Null) {for (int I = 0; I <infos. length; I ++) {// Log. d (TAG, "isNetworkAvailable-info:" + // infos [I]. toString (); if (infos [I]. getState () = NetworkInfo. state. CONNECTED) {Log. d (TAG, "isNetworkAvailable-I" + I) ;}}// if it is only used to determine the network connection, you can use cm. getActiveNetworkInfo (). isAvailable (); NetworkInfo networkInfo = cm. getActiveNetworkInfo (); if (networkInfo! = Null) {Log. d (TAG, "isNetworkAvailable-whether there is a network:" + networkInfo. isAvailable ();} else {Log. d (TAG, "isNetworkAvailable-finished without network! "); Return false;} // 1. determine whether there is a 3G network if (networkInfo! = Null & networkInfo. getType () = ConnectivityManager. TYPE_MOBILE) {Log. d (TAG, "isNetworkAvailable-3G network available"); return true;} else {Log. d (TAG, "isNetworkAvailable-No 3G network");} // 2. Determine if there is a Wi-Fi connection if (networkInfo! = Null & networkInfo. getType () = ConnectivityManager. TYPE_WIFI) {Log. d (TAG, "isNetworkAvailable-wifi connection available"); return true;} else {Log. d (TAG, "isNetworkAvailable-no wifi connection") ;}} return false ;}



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.