Data does not upload sometimes, even after network is available in Android app

Viewed 66

I am dealing with with a weird situation and pardon me if I fail in explaining.

I have an app where user uploads a form. When the network is available (either wifi or any other network), the form will be uploaded and user will see "Submitted" text on screen. If the network is not available, form won't be uploaded and it will be saved in the android local storage and user will be see a "pending" message. Technically whenever user's device is back online it will automatically uploads the form and user will see "submitted" message on screen and this is happening for majority of the users.

Once in a while form is not submitted even after the network is available, however user can do other network related stuff like sending/receiving emails. This is an edge case as this is not happening to all the users using the app and this happens once in a while.

Below is the code I am using to receive network status

 protected BroadcastReceiver networkConnectionStatusReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateNetworkIndicator();
    }
 };

Below is updateNetworkIndicator() code

 public void updateNetworkIndicator() {
    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    if (networkStatusMenuItem != null) {
        if (isConnected) {
            networkStatusMenuItem.setTitle("Online");

            if (syncMenuItem != null) {
                syncMenuItem.setEnabled(true);
            }

        } else {
            networkStatusMenuItem.setTitle("Offline");

            if (syncMenuItem != null) {
                syncMenuItem.setEnabled(false);
            }
        }
    }
}

Does anyone see anything wrong with this code? And if there is any additional info required let me know. I just wanted to know if I am using anything depreciated or missing additional check.

1 Answers

getActiveNetworkInfo() was deprecated in Android 10. Basically they don't want each app to monitor the network connectivity to determine when to do things. For this reason, Android introduced WorkManager. With WorkManager you can define a task that should take place ONLY if there is network connectivity. As soon as the network connectivity is restored, Android will execute this job.

For more info.details see https://developer.android.com/training/monitoring-device-state/connectivity-status-type and https://developer.android.com/topic/libraries/architecture/workmanager

Also, if you've got a manifest-registered <intent-filter> for android.net.conn.CONNECTIVITY_CHANGE, this will no longer get triggered on recent Android versions. See https://developer.android.com/reference/android/net/ConnectivityManager#CONNECTIVITY_ACTION for more info.

Related