What is the best way in android to check if internet is active continiously(when the internet is inactive)?

2.2k views Asked by At

I am building an android app that notifies a user when the internet connection is active again. He/she has to enable the feature when the internet is not working. What is the best way to do this? Currently I am running an infinite loop ping request to google in a service but it doesn't continue after few thousand requests. Are there any simpler or better ways to achieve this? Thank you.

Edit: Most of the answers and references check only if my device is connected to Wifi. But what if my device is connected to my router but the internet connection is not working?

3

There are 3 answers

0
W4R10CK On

I'm using broadcast to check the connection every time. Create a class for connection info.

import android.content.Context;
import android.content.ContextWrapper;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class ConnectivityStatus extends ContextWrapper{

    public ConnectivityStatus(Context base) {
        super(base);
    }

    public static boolean isConnected(Context context){

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo connection = manager.getActiveNetworkInfo();
        if (connection != null && connection.isConnectedOrConnecting()){
            return true;
        }
        return false;
    }
}

Apply code into your Activity:

 private BroadcastReceiver receiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
        if(!ConnectivityStatus.isConnected(getContext())){
            // no connection
        }else {
            // connected
        }
    }
 };

Register broadcast in your activity's onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    your_activity_context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    ..
    ...
    ....
  }

Don't forget to unregistered/register on Activity cycle:

@Override
protected void onResume() {
    super.onResume();
    your_activity_context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

}

@Override
protected void onPause() {
    super.onPause();
    your_activity_context.unregisterReceiver(receiver);

}
0
Bhoomika Patel On

You can use Broadcast receiver for continuous checking of Internet Availability. Receiver will notify you when Internet is available.

Visit this page, its help you : Broadcast receiver for checking internet connection in android app

0
harshvardhan On

Create a simple ping request to google.com and run a background thread using volley or some other network library in the activity to keep pinging. On response, check the network response code. If it is a server error code (5xx series), then your internet is unavailable. If you get OK code (2xx series) then you have a working internet connection. For both cases, keep a retry delay of 500 ms and keep collecting network availability response.

Alternatively, use the Android network manager. This class can be implemented as follows:

public class NetworkConnectivity {
    Context context;

    private Context _context;

    public NetworkConnectivity(Context context){
        this._context = context;
    }

    public boolean isNetworkConnected(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null)
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }

        }
        return false;
    }
}

Call isNetworkConnected() at every 500 ms from a background thread and use the response (true for connected and false for not connected) according to what you want to do with that knowledge about connection status.