Search code examples
javaandroidandroid-intentandroid-wifiandroid-broadcast

Android - check internet and Wi-Fi connectivity with the help of BroadcastReceiver?


I want to check internet and Wi-Fi connectivity. I have implemented the code. But in this code the problem is, when I activate Wi-Fi in my device then it continuously toasts "Wi-Fi disconnected" and "Internet Connected" 3-4 times and then at last times it toasts "Wi-Fi connected" and then my Wi-Fi connection appears enabled in my device. I need to check Wi-Fi connection only once when I enable or disable Wi-Fi. Thanks.

Java

public class ConnectionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        boolean isConnectedInternet = 
                intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

        ConnectivityManager wifiConn = 
                (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);

        NetworkInfo mWifi = wifiConn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected()) {

            Toast.makeText(context, "Wifi Conected", Toast.LENGTH_LONG).show();
        }
        else {

            Toast.makeText(context, "Wifi disconnected", Toast.LENGTH_LONG).show();
        }

        if(isConnectedInternet){
            Toast.makeText(context, "Internet Connection Lost", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(context, "Internet Connected", Toast.LENGTH_LONG).show();
        }
    }

}

Menifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<receiver android:name=".ConnectionReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
            </intent-filter>
        </receiver>

Solution

  • i have my connection you can use it..

          public class ConnectionDetector {
            private Context _context;
            public ConnectionDetector(Context context){
            this._context = context;
            }
    
            public boolean isConnectingToInternet(){
            ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null)
            {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED){
                    return true;
                 }
    
            }
            return false;
            }
    }
    

    and in your activity..

    if (new ConnectionDetector(this).isConnectingToInternet()){
             Toast.makeText(context, "Wifi Conected", Toast.LENGTH_LONG).show();
            }
            else {
    
                Toast.makeText(context, "Wifi disconnected", Toast.LENGTH_LONG).show();
            }