Android phones also support WiFi and 3G, determine the network connection, determine the network type (WiFi or 3G).
Permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Determine network connectivity
/** * 判断网络是否可用, 在使用网络请求是预先使用此方法进行检查, 以节省资源. * * @return 可用状态 */ publicstaticbooleanisNetAvailable() { Context context = App.getAppContext(); ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); returnnull && info.isAvailable() && info.isConnected()); }
Determine the network type
Connectivitymanager conman = (Connectivitymanager) context. Getsystemservice(Context. CONNECTIVITY_service);//3Gstate Mobile = Conman. Getnetworkinfo(0). GetState();Wifistate WiFi = Conman. Getnetworkinfo(1). GetState();if (mobile = = Networkinfo. State. CONNECTED|| Mobile = = Networkinfo. State. Connecting) {//TODO:inchMobile do something} else if (WiFi = = Networkinfo. State. CONNECTED|| WiFi = = Networkinfo. State. Connecting) {//TODO:inchWiFi Do something}
More Granular network types:
Telephonymanager#getnetworktype or Networkinfo#getsubtypename
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-Determine network status