Android Network Judgment Tool Class (Apn+wifi)

Source: Internet
Author: User

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)

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.