光阴似箭催人老,日月如移越少年。
Check the API I remember whether the network status is in ConnectivityManager and NetworkInfo.
https://developer.android.goo...Display mobile network status: mobile (data network), wifi
public String netstat() { try { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); if (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED) { if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { return "mobile"; } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) { WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); int ip = wm.getConnectionInfo().getIpAddress(); if (ip == 0) { return "wifi"; } else { return "wifi:" + ((ip & 0xff) + "." + (ip >> 8 & 0xff) + "." + (ip >> 16 & 0xff) + "." + (ip >> 24 & 0xff)); } } } } catch (Exception e) { e.printStackTrace(); } return "offline"; }
public static int getNetType(Context context) { int netType = -1; ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if(networkInfo==null) { return netType; } return networkInfo.getType(); }
switch(getNetType(context)){ case wifi: break //TODO }
public static boolean isWifiConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI; }
Check the API
I remember whether the network status is in ConnectivityManager and NetworkInfo.
https://developer.android.goo...
Display mobile network status: mobile (data network), wifi