Generally, for android phones, we can judge the network situation through the method provided by sdk
/** * Get the current network status: no network - 0: WIFI network 1: 4G Network - 4: 3G network - 3: 2G network - 2 * custom * * @param context * @return */ public static int getAPNType(Context context) { //Result return value int netType = 0; //Get all connection management objects of mobile phone ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //Obtain NetworkInfo object NetworkInfo networkInfo = manager.getActiveNetworkInfo(); //NetworkInfo Empty object means no network if (networkInfo == null) { return netType; } //otherwise NetworkInfo If the object is not empty, get the networkInfo Types int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_WIFI) { //WIFI netType = 1; } else if (nType == ConnectivityManager.TYPE_MOBILE) { int nSubType = networkInfo.getSubtype(); TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); //3G Unicom's 3 G by UMTS or HSDPA Telecom 3 G by EVDO if (nSubType == TelephonyManager.NETWORK_TYPE_LTE && !telephonyManager.isNetworkRoaming()) { netType = 4; } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0 && !telephonyManager.isNetworkRoaming()) { netType = 3; //2G Mobile and Unicom 2 G by GPRS or EGDE,Telecom 2 G by CDMA } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS || nSubType == TelephonyManager.NETWORK_TYPE_EDGE || nSubType == TelephonyManager.NETWORK_TYPE_CDMA && !telephonyManager.isNetworkRoaming()) { netType = 2; } else { netType = 2; } } return netType; }
Note that for the Tv project, some of the Tv (such as Xiaomi Tv) of android system support cable connection (non wifi, 2g 3g 4g). At this time, the above method will be judged as 0, no network connection status, so for the Tv project, network adaptation needs to be compatible
The solution is to ping An Internet.
/* * @category Judge whether there is an external network connection (ordinary methods cannot judge whether the external network is connected, such as connecting to the local area network) * @return */ public static final boolean ping() { String result = null; try { String ip = "www.baidu.com";// ping Can be replaced by any kind of reliable internet Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping Web site 3 times // read ping Can not add InputStream input = p.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(input)); StringBuffer stringBuffer = new StringBuffer(); String content = ""; while ((content = in.readLine()) != null) { stringBuffer.append(content); } Log.d("------ping-----", "result content : " + stringBuffer.toString()); // ping State int status = p.waitFor(); if (status == 0) { result = "success"; return true; } else { result = "failed"; } } catch (IOException e) { result = "IOException"; } catch (InterruptedException e) { result = "InterruptedException"; } finally { Log.d("----result---", "result = " + result); } return false; }
Thus, the network state can be distinguished: wired / wifi/2g/3g/4g