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 ;}