0

I am working on an Android application which requires constant listener of Internet connectivity. I am using Broadcast listener and successfully applied it. But my code only shows the Toast message.

I want to stop the current activity and show a default XML file which says "No Internet Connection". and whenever it connect the Internet, previous activity resumes.

ExampleBradcastReceiver.java

public class ExampleBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        boolean noConnectivity = intent.getBooleanExtra(
                ConnectivityManager.EXTRA_NO_CONNECTIVITY, false
        );
        if (noConnectivity) {
            Toast.makeText(context, "Disconnected", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();
        }
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
protected void onStart() {
    super.onStart();
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(exampleBroadcastReceiver, filter);
}

@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(exampleBroadcastReceiver);
}
}

In the place of Toast Message, I want to show a default XML file whenever disconnected and resume activity whenever connected.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • You can set up a fullsreen dialog when no internet connection is detected then dismiss it when connection is back – esQmo_ Aug 19 '18 at 00:21
  • Possible duplicate of [Broadcast receiver for checking internet connection in android app](https://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app) – R7G Aug 19 '18 at 04:53

2 Answers2

1

You can move ExampleBroadcastReceiver to MainActivity as an inner class. And since in Java inner classes have access to their parent classes' methods and fields, you can in onReceive method consider showing/hiding the Internet disconnected view.

public class MainActivity extends AppCompatActivity {
    ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(exampleBroadcastReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(exampleBroadcastReceiver);
    }

    private void showInternetDisconnectedView(boolean disconnected){
        // show or hide based on 'disconnected'
    }

    private class ExampleBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                boolean noConnectivity = intent.getBooleanExtra(
                    ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                showInternetDisconnectedView(noConnectivity);
            }
        }
    }
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • I thought it might be better to have one also on the application object but given that when you start the app it will immediately go to the "MainActivity" it's probably best to have this logic on the activity lifecycle and not the app's. – L7ColWinters Aug 19 '18 at 04:13
-1

You need to move Broadcast receiver code into Activity and on receiving internet connection events you can stop current in progress activity and make internet failure layout visible there only as it is part of Activity class. If it is required through out the Application, then create Base activity and handle this there to avoid duplicating code on every screen.