Mark F
Mark F

Reputation: 1543

Checking Online Connectivity Always Returns True

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();
        }
    }
}}

Upvotes: 0

Views: 87

Answers (2)

Deepesh
Deepesh

Reputation: 533

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)
    );

Upvotes: 1

M.Sameer
M.Sameer

Reputation: 3151

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.

Upvotes: 0

Related Questions