-1

Hello guys i am building an application in which i want to notify the user only when the device looses connection whether its mobile data or wifi.

So far i have used this method:

private void setupReceiver() {
        CheckNetworkStatusReceiver receiver = new CheckNetworkStatusReceiver();
        registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    }

and my Receiver as such:

public class CheckNetworkStatusReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null){
            Log.e("Action ",intent.getAction());
            if(intent.getAction().equalsIgnoreCase("android.net.conn.CONNECTIVITY_CHANGE")) {
                Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

and it works fine but the thing is that the toast also shows when it is connected to the network which is something i do not want. I only want the toast to show when its disconnects.

Any ideas?

  • follow this link will help you http://stackoverflow.com/questions/3119607/how-to-be-notified-on-wifi-network-status-change – Nandan Singh Nov 23 '16 at 09:48
  • i also try this but it didnt work –  Nov 23 '16 at 09:48
  • Check if network is available in `onReceive` method and notify user if it is not connected...[Check network](http://stackoverflow.com/questions/2326767/how-do-you-check-the-internet-connection-in-android) – lubilis Nov 23 '16 at 09:51

2 Answers2

2

you can check the network connection like this :

    public static boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) MBankApplication.appContext.getSystemService
            (Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(1);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }
    NetworkInfo mobileNetwork = cm.getNetworkInfo(0);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork == null || !activeNetwork.isConnected()) {
        return false;
    }
    return true;
}

then display your toast

    if(!isNetworkConnected()){
        Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
    }
SepJaPro2.4
  • 704
  • 9
  • 19
0

before displaying toast check if connection available some thing like this

 if(!isNetworkAvailable()){
Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
 }




private boolean isNetworkAvailable() {
 ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
 return activeNetworkInfo != null; 
}

Edit

To be on safe side use this

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
Manohar
  • 22,116
  • 9
  • 108
  • 144