Open In App

How to Use Connectivity Diagnostics API in Android?

Last Updated : 02 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When working with Android Operating Systems, your app may need to continuously need to be in talk with the internet, or perhaps a few times in a while or maybe it requires a specific condition to establish a connection with the servers, like a VPN or other service, without the presence there won’t be any communication, this is where the new Connectivity Diagnostics API comes into play. In this article, we will be learning how we can implement this newly launched API by baking in our Android apps.

Connectivity Diagnostics API in Depth

Apps that own or operate networks, such as carrier, VPN, and Wi-Fi recommendation apps, can access diagnostic network connectivity data from the framework via the Connectivity Diagnostics API. For the networks that they control or own, these apps have the ability to register callbacks and receive notifications with connectivity information. Apps won’t get notifications for networks that they don’t own or own.

Use Cases of Connectivity Diagnostics API:

  1. Wi-Fi recommendation apps: WiFi networks advise the system to use
  2. Manage cellular networks: The connection which has right, due to cellular reasons.
  3. VPN software: Control every network that their VPN accesses, but only when that VPN is operational.

We expect a callback when, the system owns a Wi-Fi network in place but is called upon by another method, like the cellular, then the callback happens to decide which one to go with, and which one to give priority to.

To check for Network Validation you can use the code below:

Java




public class gfgConnectivityArticle {
    long gfgReportTimeStamp;
    // To check for the capability 
      // the network can provide, 4G, 5G.
    NetworkCapabilities gfgNetworkLatch;
    // To check for the network.
    Network gfgNetwork;
    // To check if the network is having a link
    PersistableBundle gfgAddInformation;
}


You can use this to check multiple factors which are mentioned in the comments in the code itself, also not that you may use all of the codes at once, but ensure the user’s device does not bottleneck as calling these methods does require some significant CPU usage. 

To check if an internet stall is expected:

You can check if there is a predicted data stall, which can be caused due to some underlying issue beforehand, and get your app ready in advance by performing some tasks that are vital for its functioning. Use the code below to check:

Java




public class gfgDataReport {
    // All other methods remain stable
    // Add this last method to check stall
    // Geeks for Geeks
    PersistableBundle gfgStallNetDetails;
}


GeekTip: Because connecting to location-specific networks can reveal the user’s precise location, apps registering callbacks must have the ACCESS_FINE_LOCATION permission.

Understanding the Diagnostic Callback

An app must receive a ConnectivityDiagnosticsManager object from the platform in order to use the Connectivity Diagnostics API. To register and deregister ConnectivityDiagnosticsCallback implementations, use this instance. You can use this callback in various places to check if no-ops are callback methods that aren’t overridden. To understand the callbacks better look at the snapshots below:

If you want to prefetch the network details:

Java




public class gfgExampleNetworkCallbackOne
    extends ConnectivityDiagnosticsCallback {
    @Override
    public void onConnectivityReportAvailable(
        @NonNull ConnectivityReport report)
    {
        // Log the Data You want to use here
        Network gfgNetwork;
    }


If you want to prefetch the data stall:

Java




public class gfgExampleNetworkCallbackTwo
    extends ConnectivityDiagnosticsCallback {
    @Override
    public void
    onDataStallSuspected(@NonNull DataStallReport report)
    {
        // Log the data stalling method here.
        PersistableBundle gfgNetworkStallDetails;
    }
}


If you want to check for network connectivity:

Java




public class gfgExampleNetworkCallbackTwo
    extends ConnectivityDiagnosticsCallback {
  
    @Override
    public void onNetworkConnectivityReported(
        @NonNull Network gfgNetwork,
        boolean hasConnectivity)
    {
        // Log the Boolean here to check
        LinkProperties gfgIsNetworkPresent;
    }
}


This is how you can test for various network connections within your app with the new Android 13’s Network API, the thing which was once a nightmare, now becomes a piece of cake to work with!

Conclusion

A ConnectivityDiagnosticsManager instance must be obtained by an app from the platform in order to use the Connectivity Diagnostics API. Implementations of the ConnectivityDiagnosticsCallback should be registered and deregistered using this instance. Hope this article helped you learn how to check for network connectivity within your app, the use cases could be endless, and this API has just been introduced, so we can expect this to deepen further with further Android Versions. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads