1. Determine whether the network connection is available
Public static Boolean isnetworkavailable (context) {connectivitymanager CM = (connectivitymanager) context. getsystemservice (context. connectivity_service); If (Cm = NULL) {} else {// you can use cm if it is only used to determine the network connection. getactivenetworkinfo (). isavailable (); networkinfo [] info = cm. getallnetworkinfo (); If (info! = NULL) {for (INT I = 0; I <info. length; I ++) {If (info [I]. getstate () = networkinfo. state. connected) {return true ;}}} return false ;}
The following code not only determines if the network is not enabled, but also opens the interface for enabling the network. The specific code is as follows:
Protected Boolean checknetwork () {// todo auto-generated method stubboolean flag = false; connectivitymanager cwjmanager = (connectivitymanager) getsystemservice (context. connectivity_service); If (cwjmanager. getactivenetworkinfo ()! = NULL) Flag = cwjmanager. getactivenetworkinfo (). isavailable (); If (! Flag) {builder B = new alertdialog. builder (this ). settitle ("no available network "). setmessage ("enable GPRS or WiFi network connection"); B. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {// todo auto-generated method stubintent mintent = new intent ("/"); componentname comp = new componentname ("com. android. settings "," com. android. settings. wirelesssettings "); mintent. setcomponent (COMP); mintent. setaction ("android. intent. action. view "); startactivity (mintent );}}). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {// todo auto-generated method stub dialog. cancel ();}}). create (); B. show ();} return flag ;}
It can be used as follows:
If (! Checknetwork () return;
Ii. Determine if GPS is enabled
public static boolean isGpsEnabled(Context context) { LocationManager lm = ((LocationManager) context .getSystemService(Context.LOCATION_SERVICE)); List<String> accessibleProviders = lm.getProviders(true); return accessibleProviders != null && accessibleProviders.size() > 0; }
Iii. Determine whether WiFi is enabled
public static boolean isWifiEnabled(Context context) { ConnectivityManager mgrConn = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mgrTel = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); return ((mgrConn.getActiveNetworkInfo() != null && mgrConn .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS); }
4. Determine whether the network is a 3G network
public static boolean is3rd(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkINfo = cm.getActiveNetworkInfo(); if (networkINfo != null && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) { return true; } return false; }
5. Determine whether it is WIFI or 3G network. The user's embodiment is here. WiFi can be downloaded or played online.
public static boolean isWifi(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkINfo = cm.getActiveNetworkInfo(); if (networkINfo != null && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; }