For example, they turned on the Internet, displayed a corresponding message, then disconnected it and again received a corresponding message.

Those monitor the status of the Internet at any time, and not a one-time

    2 answers 2

    You do not need to monitor the state of the Internet , but receive notifications about its change

    Determining the connection status at time:

    boolean checkInternet(Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return activeNetwork.isConnectedOrConnecting(); } 

    BroadcastReceiver on network status change:

     public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { if(checkInternet(context)){ Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show(); } } 

    And in the manifest to determine the receiver itself:

      <receiver android:name="<ваш.пакет.сюда>.NetworkChangeReceiver "> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> </intent-filter> </receiver> 

    And be sure to get the status of the network:

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

      Get the ConnectivityManager and select which broadcasts we’ll listen to:

       connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); BroadcastReceiver connectivityStatusReceiver = new NetworkConnectionStatusReceiver(); 

      When you need to start monitoring the state of the Internet, we launch a wiretap of the corresponding intents:

       context.registerReceiver(connectivityStatusReceiver, NETWORK_INTENT_FILTER)) 

      Well sobsno check the availability of the Internet:

       protected boolean hasConnection() { NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnected()) { switch (netInfo.getType()) { case ConnectivityManager.TYPE_MOBILE: case ConnectivityManager.TYPE_WIFI: case ConnectivityManager.TYPE_WIMAX: return true; default: return false; } } return false; } private class NetworkConnectionStatusReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (hasConnection()) { // network available } else { // no network }; } } 
      • Only very often the network continues to be connected, but the Internet is no longer in practice. - Sergey Rufanov
      • Well, yes, if there is more than one network connection on the devices (which is 99% of the time) - hasConnection will always return true, even if the network interface is disconnected through which it was connected to the Internet. Now you are not checking to connect to the Internet at all, but simply the presence of at least one connected network interface. - Sergey Rufanov
      • one
        To accomplish the task, you need at least to add code in hasConnection that will try to connect to some Internet resource. - Sergey Rufanov
      • @SergeyRufanov I gave the fastest and easiest way to check, which in many cases is enough. If there is a need to check availability more carefully, then it would be most logical if the current method receives a signal that there is a connection - try to perform some simple request to your API (if any), you can make a HEAD request for optimization. But the implementation of this request will depend on how the application implements the work with the API, and therefore I do not think it makes any sense to give examples here, too many options. - xkor