0

I have a dynamic Broadcast Receiver set up in my Main Activity that should display a Toast message whenever my Network State Changes. I connect online, run the application, the "You are online!" Toast message displays, I then turn the internet off on my laptop, navigate back to my Main Activity and I keep getting the same "You are online!" Toast. I'm checking my log statements and my NetworkInfo Object Keeps returning that I am indeed online when I'm clearly not. I tried this on my emulator and my phone. Does anyone know why?

Here is my Main Activity which contains my nested Broadcast Receiver Class, Intent Filters and Method to execute the Toast:

public class MainActivity extends AppCompatActivity {
private IntentFilter onlineIntentFilter;
private CheckOnlineReceiver checkOnlineReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    onlineIntentFilter = new IntentFilter();
    checkOnlineReceiver = new CheckOnlineReceiver();
    onlineIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

    }


@Override
protected void onResume() {
    super.onResume();
    registerReceiver(checkOnlineReceiver, onlineIntentFilter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(checkOnlineReceiver);
}


public void isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    Log.v("TAG", "NETWORK INFO =========== " + networkInfo);
    if (networkInfo != null && networkInfo.isConnected()) {
        Toast.makeText(this,"You Are Online!",Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this,"You Are Not Online!",Toast.LENGTH_LONG).show();
    }
}
private class CheckOnlineReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        boolean stateChanged = (action.equals(ConnectivityManager.CONNECTIVITY_ACTION));
        if (stateChanged){
            isNetworkAvailable();
        }
    }
}}
Mark F
  • 1,523
  • 3
  • 23
  • 41

2 Answers2

1

This is how I will do it:

private class CheckOnlineReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive action: " + intent.getAction());
        Log.d(TAG, "onReceive component: " + intent.getComponent());
        NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        stateChanged = networkInfo != null && networkInfo.isConnectedOrConnecting();

    if (stateChanged) {
        Toast.makeText(this,"You Are Online!",Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this,"You Are Not Online!",Toast.LENGTH_LONG).show();
       }
    }
}

Replace your receiver code with the following:

registerReceiver(
            checkOnlineReceiver,
            new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
    );
Deepesh
  • 523
  • 4
  • 11
0

Did you try to turn WiFi off on the emulator and see what happens ? networkInfo.isConnected() does not check for Internet. It just means there is an active connection.

M.Sameer
  • 3,072
  • 1
  • 24
  • 37
  • Is there another method I should be using? – Mark F Feb 19 '17 at 12:24
  • 1
    What I do myself is what Google and Apple do to check for Internet connectivity on Android and iOS respectively : try to reach a web service that sends you a specific response code. If it fails, there is no Internet connection. If you got what you expect, there is Internet connection. If you got different response from what you expect then there is a Captive portal in the middle. – M.Sameer Feb 19 '17 at 12:46
  • @MarkF Check out the accepted answer in this similar question : http://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection – M.Sameer Feb 19 '17 at 12:50