Tag:android network wifi apn
public class Networkhelper {private static String Log_tag = "Networkhelper";p ublic static uri uri = Uri.parse ("content://t Elephony/carriers ")/** * Determine if there is a network connection */public static Boolean isnetworkavailable (context context) {Connectivitymanager Connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); if (connectivity = = NULL {LOG.W (Log_tag, "couldn ' t get Connectivity Manager");} else {networkinfo[] info = connectivity.getallnetworkinfo (); if (info! = null) {for (int i = 0; i < info.length; i++) {i F (info[i].isavailable ()) {LOG.D (Log_tag, "Network is available"); return true;}}} LOG.D (Log_tag, "Network is not available"); return false;} public static Boolean Checknetstate (Context context) {Boolean netstate = false; Connectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); (Connectivity! = NULL) {networkinfo[] info = connectivity.getallnetworkinfo (); if (info! = null) {for (int i = 0; i < info.length;i++) {if (info[i].getstate () = = NetworkInfo.State.CONNECTED) {netstate = True;break;}}}} return netstate; }/** * Determine if the network is roaming */public static Boolean isnetworkroaming (context context) {Connectivitymanager connectivity = (Connectiv Itymanager) Context.getsystemservice (Context.connectivity_service), if (CONNECTIVITY = = null) {LOG.W (Log_tag, " Couldn ' t get Connectivity Manager ');} else {Networkinfo info = connectivity.getactivenetworkinfo (); if (info! = null&& Info.gettype () = = Connectivitymanager.type_mobile) {Telephonymanager TM = (Telephonymanager) context.getsystemservice ( Context.telephony_service), if (tm! = null && tm.isnetworkroaming ()) {LOG.D (Log_tag, "Network is roaming"); return true;} else {log.d (Log_tag, "Network is not Roaming");}} else {log.d (Log_tag, "not using mobile network");}} return false;} /** * Determine if mobile network is available * * @param context * @return * @throws Exception */public Static Boolean ismobiledataenable (context Context) throws Exception {Connectivitymanager ConnecTivitymanager = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Boolean ismobiledataenable = False;ismobiledataenable = Connectivitymanager.getnetworkinfo (ConnectivityManager.TYPE_MOBILE ). Isconnectedorconnecting (); return ismobiledataenable;} /** * Determine if WiFi is available * @param context * @return * @throws Exception */public Static Boolean iswifidataenable (context context) Throws Exception {Connectivitymanager Connectivitymanager = (connectivitymanager) context.getsystemservice ( Context.connectivity_service); Boolean iswifidataenable = False;iswifidataenable = Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi). isconnectedorconnecting (); return iswifidataenable;} /** * Set MOBILE network Switch * * @param context * @param enabled * @throws Exception */public static void setmobiledataenabled (Cont Ext context, Boolean enabled) throws Exception {Apnmanager apnmanager=apnmanager.getinstance (context); list<apn> list = Apnmanager.getapnlist (), if (enabled) {for (APN Apn:liST) {contentvalues CV = new Contentvalues (); Cv.put ("APN", APNMANAGER.MATCHAPN (APN.APN)); Cv.put ("type", APNMANAGER.MATCHAPN (Apn.type)); Context.getcontentresolver (). Update (URI, CV, "_id=?", new string[] {Apn.apnid});} else {for (APN apn:list) {contentvalues CV = new Contentvalues () cv.put ("APN", APNMANAGER.MATCHAPN (APN.APN) + "Mdev"); CV . put ("type", APNMANAGER.MATCHAPN (Apn.type) + "Mdev"), Context.getcontentresolver (). Update (URI, CV, "_id=?", New String [] {Apn.apnid});}}}
Android Network Judgment Tool Class (Apn+wifi)