android – How to constantly check if there is an Internet connection?


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

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


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

Determining the connection state at a point in time:

boolean checkInternet(Context context) {   

    ConnectivityManager cm =

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork.isConnectedOrConnecting();

BroadcastReceiver on network state change:

public class NetworkChangeReceiver extends BroadcastReceiver {

public void onReceive(final Context context, final Intent intent) {

         Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show(); 


And in the manifest, define the receiver itself:

        android:name="<ваш.пакет.сюда>.NetworkChangeReceiver ">
            <action android:name="" />
            <action android:name="" />

And be sure to have permission to get the state of the network:

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