Search code examples
androidbroadcastreceiver

BroadcastReceiver for Internet checking


I have Broadcast Receiver for doing Internet checking. Every time device is not connected with Internet, always getting 2 output. I just want one output.


BroadcastReceiver

public class Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    if (!isConnected){
        Log.i("INTERNET","------------------------- not connect");
    }
}

}


And this is the output

12659-12659 I/ViewRootImpl: CPU Rendering VSync enable = true
12659-12659 I/ViewRootImpl: CPU Rendering VSync enable = true
12659-12701 V/RenderScript: Application requested CPU execution
12659-12701 V/RenderScript: 0xb8116c40 Launching thread(s), CPUs 4
12659-12659 I/INTERNET: ------------------------- not connect
12659-12659 I/INTERNET: ------------------------- not connect

Solution

  • try this

    create one class

    public class ConnectivityReceiver extends BroadcastReceiver {

    public static ConnectivityReceiverListener connectivityReceiverListener;
    
    public ConnectivityReceiver() {
        super();
    }
    
    @Override
    public void onReceive(Context context, Intent arg1) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();
    
        if (connectivityReceiverListener != null) {
            connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
        }
    }
    
    public static boolean isConnected() {
        ConnectivityManager
                cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();
    }
    
    
    public interface ConnectivityReceiverListener {
        void onNetworkConnectionChanged(boolean isConnected);
    }
    

    }

    create one another class

    public class MyApplication extends Application {
    
    private static MyApplication mInstance;
    
    
    
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }
    
    public static synchronized MyApplication getInstance() {
        return mInstance;
    }
    
    public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
        ConnectivityReceiver.connectivityReceiverListener = listener;
    }
    

    }

    add permisiion in maenfiest file

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

    **and this **

    **

    and dont forget to add this in manifiest

     <receiver
            android:name="com.ncrypted.redfox.Utils.ConnectivityReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
    

    now call the where you want to check internet like this

    implement this in your activity

     ConnectivityReceiver.ConnectivityReceiverListener
    
     @Override
    public void onNetworkConnectionChanged(boolean isConnected) {
        showSnack(isConnected);
    }
     private void showSnack(boolean isConnected) {
        if (isConnected) {
            getUserStatus();
        } else {
            Toast.makeText(this, getString(R.string.str_internet_err), Toast.LENGTH_SHORT).show();
        }
    }
    

    for more information please follow this link