amir
amir

Reputation: 13

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

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!

Upvotes: 0

Views: 106

Answers (1)

Prince Dholakiya
Prince Dholakiya

Reputation: 3401

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.

Upvotes: 0

Related Questions