When to check your internet connection in mobile apps

I would like to know the recommended and best practices for checking Internet connectivity in mobile applications.

In most applications that I developed with my teammates, we check the Internet connection before any actions that require an Internet connection. But I see many applications that notify when the device is disconnected from the Internet.

I would like to get a clear idea about this topic, and I believe that many developers such as myself are hesitant about how to properly perform this check when they are developing the application.

Any help or ideas would be appreciated.

+4
source share
3 answers

You can use the broadcast receiver to handle Wi-Fi changes.

Receiver Code:

private BroadcastReceiver WifiStateChangedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

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

        switch (extraWifiState) {
            case WifiManager.WIFI_STATE_DISABLED: {
                handler.sendEmptyMessage(DATA_DISCONNECTED);
            }
            break;
            case WifiManager.WIFI_STATE_DISABLING: {
            }
            break;
            case WifiManager.WIFI_STATE_ENABLED: {
                handler.sendEmptyMessage(DATA_CONNECTED);
            }
            break;
            case WifiManager.WIFI_STATE_ENABLING: {
            }
            break;
            case WifiManager.WIFI_STATE_UNKNOWN: {
            }
            break;
        }
    }
};

Receiver Registration:

this.registerReceiver(this.WifiStateChangedReceiver,
            new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));

To check for changes in the mobile data connection, you can use TelephonyManager:

PhoneStateListener pslCell = new PhoneStateListener() {
        @Override
        public void onDataConnectionStateChanged(int state) {
            switch (state) {
                case TelephonyManager.DATA_DISCONNECTED: {
                    handler.sendEmptyMessage(DATA_DISCONNECTED);
                }
                break;
                case TelephonyManager.DATA_SUSPENDED: {
                    handler.sendEmptyMessage(DATA_CONNECTED);
                }
                break;
            }
        }
    };

    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(pslCell, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);

I recommend that you keep the network status in static variables. You also need to check the status of the network that each application runs.

+3
source

It is always connected with the project that we are doing. If you run only an application that requires only an Internet connection during any action, then checking your Internet connection before proceeding with an action is better, it will remove the overhead of checking your Internet connection every time. I prefer this method

, -, . Api Android BroadCast Recivers

+2

, .

:

1) , .

2) .

3) We need an immediate response to the status of the Internet connection, so our application constantly checks it and immediately sends us a notification about the violation of Internet failure or success, so we can solve our application in such a scenario.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1544709/


All Articles