This example for you to share the Android decision is WiFi or 4G network code for your reference, the specific content as follows
Package com.chengxinet.bobo.utils;
Import Android.content.Context;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;
Import Android.telephony.TelephonyManager;
/** * Created by the Administrator on 2016/1/7. */public class Networkutils {public static Boolean isnetworkavailable (context C) {context-context = C.getapplicat
Ioncontext (); Get all connection management objects on the phone (including the management of connections to wi-fi,net) Connectivitymanager Connectivitymanager = (Connectivitymanager)
Context.getsystemservice (Context.connectivity_service);
if (Connectivitymanager = = null) {return false;
else {//Get Networkinfo object networkinfo[] Networkinfo = Connectivitymanager.getallnetworkinfo (); if (networkinfo!= null && networkinfo.length > 0) {for (Networkinfo anetworkinfo:networkinfo) {//
SYSTEM.OUT.PRINTLN (i + "= = = State = =" + networkinfo[i].getstate ());
SYSTEM.OUT.PRINTLN (i + "= = = Type = = =" + Networkinfo[i].gettypename ()); JudgeWhether the current network state is a connection state if (anetworkinfo.getstate () = = NetworkInfo.State.CONNECTED) {return true;
}}} return false;
/** * Determine if WiFi is open * @param context * @return/public static Boolean iswifienabled
Connectivitymanager mgrconn = (connectivitymanager) context. Getsystemservice (Context.connectivity_service);
Telephonymanager Mgrtel = (telephonymanager) context. Getsystemservice (Context.telephony_service); Return ((Mgrconn.getactivenetworkinfo ()!= null && mgrconn. Getactivenetworkinfo (). GetState () = = Networkin Fo. state.connected) | |
Mgrtel. Getnetworktype () = = Telephonymanager.network_type_umts); /** * Determine if it is a 3G network * @param context * @return/public static Boolean is3rd (context) {Connec
Tivitymanager cm = (connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo= Cm.getactivenetworkinfo ();
if (networkinfo!= null && networkinfo.gettype () = = Connectivitymanager.type_mobile) {return true;
return false; /** * Determine whether WiFi or 3g network * @param context * @return/public static Boolean Iswifi (context context) {C
Onnectivitymanager cm = (connectivitymanager) context. Getsystemservice (Context.connectivity_service);
Networkinfo networkinfo = Cm.getactivenetworkinfo ();
if (networkinfo!= null && networkinfo.gettype () = = Connectivitymanager.type_wifi) {return true;
return false;
}
}
The above is the entire contents of this article, I hope to learn more about Android software programming help.