Daily#2. Handling Network Connectivity Issues in Android Apps

 When developing an Android app, it's important to ensure your app can handle network connectivity issues, especially when switching between Wi-Fi and mobile data. Android provides APIs to monitor network connectivity and report issues to help you manage your app's behavior in different network situations.
  1. Register a `ConnectivityManager.NetworkCallback` to monitor network changes:
    private ConnectivityManager connectivityManager;
    private ConnectivityManager.NetworkCallback networkCallback;
    
    private void registerNetworkCallback(Context context) {
        connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                // Handle network connection established
            }
    
            @Override
            public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities nc) {
                // onCapabilitiesChanged
                boolean valid = nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
            }
    
            @Override
            public void onLost(Network network) {
                // Handle network connection lost
            }
        };
    
        NetworkRequest.Builder builder = new NetworkRequest.Builder();
        connectivityManager.registerNetworkCallback(builder.build(), networkCallback);
    }     
  2. Don't forget to unregister the `NetworkCallback` when it's no longer needed, such as in your Activity's `onDestroy()` method:
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connectivityManager != null && networkCallback != null) {
            connectivityManager.unregisterNetworkCallback(networkCallback);
        }
    }
  3. Report network connectivity problems using ConnectivityManager.reportNetworkConnectivity():
    private void reportNetworkConnectivity(Context context, boolean hasProblem) {
        Network activeNetwork = connectivityManager.getActiveNetwork();
        if (activeNetwork != null) {
            connectivityManager.reportNetworkConnectivity(activeNetwork, hasProblem);
        }
    }   

With these code snippets, your app can monitor and respond to network connectivity changes and provide a better user experience by handling Wi-Fi and mobile data transitions more gracefully. Remember to request the ACCESS_NETWORK_STATE permission in your AndroidManifest.xml file to use the ConnectivityManager APIs:
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Comments

Popular posts from this blog

Daily#6. Understanding and Using NetworkCallback in Android

Daily#11. Establishing Wi-Fi Connection using WifiNetworkSpecifier and WifiNetworkSuggestion

Daily#14. Understanding JVM, Dalvik, and ART: The Engines Behind Java and Android Applications