0

I am using Broadcast receiver for Internet availability. Here is the code

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
        boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

        NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

        if (!cd.isConnectingToInternet()) {
            // Internet Connection is not present
            alert.showAlertDialog(DashBoardActivity.this,
                    "Internet Connection Error",
                    "Please connect to working Internet connection", false);
            // stop executing code by return
            return;
        }

        if(currentNetworkInfo.isConnected()){
            Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
        }
    }
};

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    this.registerReceiver(this.mConnReceiver,
            new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    Toast.makeText(getApplicationContext(), "In resume", Toast.LENGTH_LONG).show();
}

When I switch on the WiFi onReceive is called only once but when I switch off the WiFi onReceive is called twice because of that pop up or Toast used in above code appears twice.

Can anyone help on this strange error?

Bunny
  • 237
  • 2
  • 13

3 Answers3

0
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
        activeNetworkInfo = connManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) {a
            Toast.makeText(getApplicationContext(),"Connected",Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
            }
    }
YaC King
  • 109
  • 1
  • 8
0

I guess you've not unregistered the receiver whenever your activity is getting paused.

Add this to your Activity

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    this.unregisterReceiver(this.mConnReceiver);
    Toast.makeText(getApplicationContext(), "In pause", Toast.LENGTH_LONG).show();
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • when I am on the same screen and switching off and switching on the router this bug comes.And also if i am directly switch off and switch on wifi from status bar.So in short it is not going in `onPause()`.When I am navigating in application it goes in `onPause` and may be because of that it comes for once only in case wifi off.so what is the problem? – Bunny Sep 22 '15 at 10:22
  • @Bunny Put a `Log.d(...)` inside your `onReceive` and check again whether it logs to logcat twice or not. – frogatto Sep 22 '15 at 10:58
  • As I'm not doing any changes so why should I put `Log.d(...)` and check again.what will happen if I put this?It's gonna give same result I guess. – Bunny Sep 22 '15 at 11:40
  • @Bunny So, how did you find the `onReceive` is called twice? – frogatto Sep 22 '15 at 11:42
0

As I understand There may be More than one State of WiFi when we switchoff wifi in Device.

Check the states of wifi using Following code and take action accordingly:

int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);

    switch (extraWifiState)
    {
    case WifiManager.WIFI_STATE_DISABLED:
    case WifiManager.WIFI_STATE_DISABLING:
        enableUI(false);
        break;
    case WifiManager.WIFI_STATE_ENABLED:
        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
        while(conMan.getActiveNetworkInfo() == null || conMan.getActiveNetworkInfo().getState() != NetworkInfo.State.CONNECTED)
        {
            try
            {
                Thread.sleep(500);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        update();
        enableUI(true);
        break;
    case WifiManager.WIFI_STATE_ENABLING:
        break;
    case WifiManager.WIFI_STATE_UNKNOWN:
        break;
    }

}

may it help You to find out to solution for your problem.

Hradesh Kumar
  • 1,765
  • 15
  • 20