Android Tools Class (2) Networkhelper Network tool class

Source: Internet
Author: User



Import Android.content.context;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.net.uri;import Android.net.networkinfo.state;import Android.telephony.telephonymanager;import    Android.util.log;public class Networkhelper {public static final int networn_none = 0;    public static final int networn_wifi = 1; public static final int networn_mobile = 2;private static String Log_tag = "Networkhelper";p ublic static uri uri = Uri.par SE ("content://telephony/carriers");/** * Infer if there is a network */public static Boolean isnetworkavailable (context context) { Connectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); (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;} /** * Check network status * @param context * @return */public static Boolean checknetstate (Context context) {Boolean netstate = Fals E 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 (INF O[i].getstate () = = NetworkInfo.State.CONNECTED) {netstate = True;break;}}}}    return netstate; }/** * Infer 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 {Lo G.D (Log_tag, "Network is not Roaming");}} else {log.d (Log_tag, "not using mobile network");}} return false;} /** * Infer if mobile network is available * * @param context * @return * @throws Exception */public Static Boolean ismobiledataenable (context Context) {Connectivitymanager Connectivitymanager = (connectivitymanager) context.getsystemservice ( Context.connectivity_service); Boolean ismobiledataenable = False;ismobiledataenable = Connectivitymanager.getnetworkinfo (Connectivitymanager.type_mobile). isconnectedorconnecting (); return ismobiledataenable;}  /** * Infer if WiFi is available * @param context * @return * @throws Exception */public Static Boolean iswifidataenable (context context) {Connectivitymanager Connectivitymanager = (connectivitymanager) context.getsystemservice (Context.CONNECTIVITY_ SERVICE); Boolean iswifidataenable = False;iswifidataenable = Connectivitymanager.getnetworkinfo (connectivitymAnager.    Type_wifi). isconnectedorconnecting (); return iswifidataenable;} public static int Getnetworkstate (context context) {Connectivitymanager Connmanager = (connectivitymanager) Context        . Getsystemservice (Context.connectivity_service);        Wifi state state = Connmanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if (state = = State.connected| |        state = = state.connecting) {return networn_wifi;        }//3g state = Connmanager.getnetworkinfo (connectivitymanager.type_mobile). GetState (); if (state = = State.connected| |        state = = state.connecting) {return networn_mobile;    } return Networn_none; }}


Android Tools Class (2) Networkhelper Network tool class

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.