1. Add permissions in
<uses-permission android:name=".ACCESS_NETWORK_STATE" /> <uses-permission android:name="" />
2. NetUtilSS network judgment tool class
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class NetUtilSS { // No connection public static final int NETWORN_NONE = 0; // Wifi connection public static final int NETWORN_WIFI = 1; // Mobile phone network data connection public static final int NETWORN_2G = 2; public static final int NETWORN_3G = 3; public static final int NETWORN_4G = 4; public static final int NETWORN_MOBILE = 5; private NetUtilSS() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * Determine whether the network is connected * * @param context * @return */ public static boolean isConnected(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); // If it is only used to determine network connection if (null != connectivity) { NetworkInfo info = (); if (null != info && ()) { if (() == ) { return true; } } } return false; } /** * Determine whether GPS is on * * @param context * @return */ public static boolean isGpsEnabled(Context context) { LocationManager lm = ((LocationManager) context .getSystemService(Context.LOCATION_SERVICE)); List<String> accessibleProviders = (true); return accessibleProviders != null && () > 0; } /** * Determine whether it is a 3G network * @param context * @return */ public static boolean is3rd(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkINfo = (); if (networkINfo != null && () == ConnectivityManager.TYPE_MOBILE) { return true; } return false; } /** * To determine whether it is wifi or 3g network, the user's embodyingness is here, and wifi can be recommended to download or play online. * * @param context * @return */ public static boolean isWifi(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkINfo = (); if (networkINfo != null && () == ConnectivityManager.TYPE_WIFI) { return true; } return false; } /** * Open the network settings interface */ public static void openSetting(final Activity activity) { ("netutils", "I'm a network 1111"); final builder = new (activity); ("Open the Internet Service"); ("The network is not connected, please go to settings to set the network!"); ("Sure", new () { public void onClick(DialogInterface dialog, int which) { if (.SDK_INT > 10) { // Open the setting interface above 3.0 or above, you can also use ACTION_WIRELESS_SETTINGS to open it to the wifi interface directly (new Intent( .ACTION_SETTINGS)); } else { (new Intent( .ACTION_WIRELESS_SETTINGS)); } (); } }); ("Cancel", new () { @Override public void onClick(DialogInterface dialog, int which) { (); ("netutils", "I'm the Internet"); } }); (); } /** * Return to the current network connection type * * @param context context * @return */ public static int getNetworkState(Context context) { ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (null == connManager) return NETWORN_NONE; NetworkInfo activeNetInfo = (); if (activeNetInfo == null || !()) { return NETWORN_NONE; } // Wifi NetworkInfo wifiInfo = (ConnectivityManager.TYPE_WIFI); if (null != wifiInfo) { state = (); if (null != state) if (state == || state == ) { return NETWORN_WIFI; } } // network NetworkInfo networkInfo = (ConnectivityManager.TYPE_MOBILE); if (null != networkInfo) { state = (); String strSubTypeName = (); if (null != state) if (state == || state == ) { switch (()) { case TelephonyManager.NETWORK_TYPE_GPRS: // Unicom 2g case TelephonyManager.NETWORK_TYPE_CDMA: // Telecom 2g case TelephonyManager.NETWORK_TYPE_EDGE: // Move 2g case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: return NETWORN_2G; case TelephonyManager.NETWORK_TYPE_EVDO_A: // Telecom 3g case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: return NETWORN_3G; case TelephonyManager.NETWORK_TYPE_LTE: return NETWORN_4G; default://Organic model returns 16,17 //China Mobile Unicom Telecom Three 3G models if (("TD-SCDMA") || ("WCDMA") || ("CDMA2000")) { return NETWORN_3G; } else { return NETWORN_MOBILE; } } } } return NETWORN_NONE; } }
3. IntentReceiver network monitoring tool class
import ; import ; import ; import ; import ; public class IntentReceiver extends BroadcastReceiver { private boolean isnet ; @Override public void onReceive(Context context, Intent intent) { ConnectivityManager manager = (ConnectivityManager)(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = (); if (isnet == true){ if (activeNetworkInfo != null && ()){ (context).show("Network connection is successful",1000); (context,"network","There is a network"); isnet = false; }else{ (context).show("The Internet is gone",1000); (context,"network","No Internet"); isnet = true; } }else{ isnet = true; } isnet = true; } }
4. BaseActivity
import ; import ; import ; import ; import ; import ; import ; public class BaseActivity extends AppCompatActivity { private BroadcastReceiver receiver = new IntentReceiver(); @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_base); IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); (receiver,filter); boolean connected = (this); if (connected) { boolean wifi = (this); boolean rd = NetUtilSS.is3rd(this); if (wifi) { (this, "WIFI is already connected", Toast.LENGTH_SHORT).show(); (this,"network","There is a network"); } else if (rd) { (this, "Mobile traffic is already connected", Toast.LENGTH_SHORT).show(); (this,"network","There is a network"); } else { (this, "Network connection is not available, please check network settings", Toast.LENGTH_SHORT).show(); (this,"network","No Internet"); // ((Activity) mContext); } } else { (this, "Network connection is not available, please check network settings", Toast.LENGTH_SHORT).show(); (this,"network","No Internet"); // ((Activity) mContext); } } public static boolean isNetwork(Context context){ String network = (context, "network"); if (("There is a network")){ return true; } return false; } @Override protected void onDestroy() { (); unregisterReceiver(receiver); } }
5. MainActivity
import ; import ; import ; import ; import ; import ; public class MainActivity extends BaseActivity { String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); if (isNetwork(this)){ (TAG,"There is a network"); //ask //.... }else { (TAG,"No Internet"); //hint //.... } } }
This is the article about the introduction of Android network monitoring and network judgment examples. For more related content on Android network monitoring and network judgment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!