3

In our app, we would like the ability to continually check for connectivity status, and if it goes down, throw a Toast or some kind of UI to indicate to the user that it's down. I tried doing this in our Retrofit Api-Builder class whenever API calls are made, but it doesn't seem to like Toasts there (causes crashes).

iOS has a monitoring capability, but it seems on Android, we have to check manually. Is there a simple/clean way of doing this across our app, given there are multiple Activities, etc?

svguerin3
  • 2,433
  • 3
  • 29
  • 53

3 Answers3

0

You can register a broadcast receiver that will be told of connectivity changes.

Information is under the Monitor for Changes in Connectivity header. https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

TrevJonez
  • 949
  • 8
  • 12
  • I did see that before, but I just wasn't sure as to how to specifically implement it. I don't see the "CONNECTIVITY_ACTION" available when I implement a manifest entry. – svguerin3 Aug 10 '17 at 03:17
  • From api 24 and up receivers of `CONNECTIVITY_ACTION` registered implicitly in the manifest don't get called. As such you should do it via `Context.registerReceiver`. All of this is in the docs that I linked in the original answer. – TrevJonez Aug 16 '17 at 22:13
0

Use Broadcast receiver so that it triggers whenever connection change takes place

Daniel Alder
  • 5,031
  • 2
  • 45
  • 55
Bapusaheb Shinde
  • 839
  • 2
  • 13
  • 16
  • I've looked into that, but there is nothing out there that is solid, which tells an app when the connection changes. "CONNECTIVITY_ACTION" does not exist when I try to implement it into my manifest. What am I missing? – svguerin3 Aug 10 '17 at 04:10
  • have you defined receiver in manifest file – Bapusaheb Shinde Aug 10 '17 at 04:40
0

This is how I did to monitor the internet connection status. Create a Java class and name it as NetworkStateChangeReceiver.

NetworkStateChangeReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import static android.content.Context.CONNECTIVITY_SERVICE;

public class NetworkStateChangeReceiver extends BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = "yourapp.packagename.NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";

@Override
public void onReceive(Context context, Intent intent) {
    Intent networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
    networkStateIntent.putExtra(IS_NETWORK_AVAILABLE,  isConnectedToInternet(context));

LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
}

private boolean isConnectedToInternet(Context context) {
    try {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            return networkInfo != null && networkInfo.isConnected();
        }
        return false;
    } catch (Exception e) {
        Log.e(NetworkStateChangeReceiver.class.getName(), e.getMessage());
        return false;
    }
}
}

In your MainActivity, add the following lines.

IntentFilter intentFilter = new 
IntentFilter(NetworkStateChangeReceiver.NETWORK_AVAILABLE_ACTION);
    LocalBroadcastManager.getInstance(this).registerReceiver(new 
BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false);
            String networkStatus = isNetworkAvailable ? "Connected!" : "Disconnected!";

            final SweetAlertDialog CC = new SweetAlertDialog(MainActivity.this, SweetAlertDialog.WARNING_TYPE);
            CC.setTitleText("Network Status");
            CC.setContentText(networkStatus);
            CC.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            CC.setCancelable(false);
            CC.show();

You could use a simple alertdialog or a toast to show the message. I had used the SweetAlertDialog Library for cleaner UI.

Abhi
  • 3,431
  • 1
  • 17
  • 35