How do I check mobile data connection and set up an event for it?

108 Views Asked by At

I want to check the mobile data connection and if it turns on or off, I will define an event for it. When the data cell is turned on, display the mobile data icon, When the mobile data is turned off, the mobile data icon will also be hidden!

1

There are 1 best solutions below

0
Prince Dholakiya On

Determine whether you have an internet connection

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

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

Determine the type of internet connection

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isMetered = cm.isActiveNetworkMetered();

For more information click here.