androidofflineandroid-connectivitymanagerandroid-network-security-confignetworkinfo

how to check for NO INTERNET in devices below API level 21


This is my MainActivity, I have registerd my network call back in the onstart. In this code onUnavailable() is never called no matter the device is turned on to the internet wifi or not. I want to check for the internet after clicking a button it will proceed to next activity if internet is connected and otherwise not

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    ConnectivityManager.NetworkCallback networkCallback;
    ConnectivityManager manager;
    public static final String TAG = "bantai";
    private NetworkRequest networkRequest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.internetTextview);
        networkRequest = new NetworkRequest.Builder().build();
        manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);


        networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
                Log.d(TAG, "onAvailable: ");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
                        textView.setText("onAvailable");
                    }
                });

            }

            @Override
            public void onLosing(@NonNull Network network, int maxMsToLive) {
                super.onLosing(network, maxMsToLive);
                Log.d(TAG, "onLosing: ");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
                        textView.setText("loosing");
                    }
                });
            }

            @Override
            public void onLost(@NonNull Network network) {
                super.onLost(network);
                Log.d(TAG, "onLost: ");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
                        textView.setText("lost");
                    }
                });            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
                Log.d(TAG, "onUnavailable: ");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
                        textView.setText("unavailable");
                    }
                });            }

            @Override
            public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
                super.onCapabilitiesChanged(network, networkCapabilities);
                Log.d(TAG, "onCapabilitiesChanged: ");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
                        textView.setText("capability");
                    }
                });            }

            @Override
            public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
                super.onLinkPropertiesChanged(network, linkProperties);
                Log.d(TAG, "onLinkPropertiesChanged: ");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
                        textView.setText("link");
                    }
                });            }

            @Override
            public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {
                super.onBlockedStatusChanged(network, blocked);
                Log.d(TAG, "onBlockedStatusChanged: ");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onAvailable: "+ Thread.currentThread().getName());
                        textView.setText("blocked");
                    }
                });            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        manager.requestNetwork(networkRequest,networkCallback);

    }

    @Override
    protected void onStop() {
        super.onStop();
        manager.unregisterNetworkCallback(networkCallback);
    }

Also ConnectivityManager.requestNetwork(networkRequest, networkCallback, 3000) this command requires Api level 21 + with a timeout of 3 seconds.


Solution

  • for checking internet easily use this function, it is responsive on api 16 and above :

    in AndroidManifest.xml add :

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

    now for checking network status :