0

I have successfully used the answer to this question to perform a one off check if mobile data is enabled, but I would like to receive a notification of such change, for example when a user goes to this screen:

Mobile Data Enable screen

Is this possible?

Community
  • 1
  • 1
joshcomley
  • 28,099
  • 24
  • 107
  • 147
  • Maybe you can use SettingsContentObserver like described in this post: http://stackoverflow.com/questions/6896746/android-is-there-a-broadcast-action-for-volume-changes – Okas Nov 05 '14 at 11:33
  • Good thinking, but I followed your idea and found ways to detect changes in on/off of bluetooth, airplane mode, GPRS, all sorts but *not* mobile data :( – joshcomley Nov 05 '14 at 18:33

2 Answers2

1

You can use OnNetworkActiveListener to monitor activation of network data. The ConnectivityManager class can give you infos on the currently active network connection. It would go like this :

ConnectivityManager cm = getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfos networkInfos = cm.getActiveNetworkInfo();

if(networkInfos.isConnected()){
    //Do something
} else {
    //Do something else
}

Hope it helps.

user3476114
  • 634
  • 1
  • 7
  • 17
0
public static boolean isNetworkOn(Context context, int networkType) {
        final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT >= 21) {
            final Network[] networks = connectivityManager.getAllNetworks();
            for (Network network : networks) {
                final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
                if (networkInfo.getType() == networkType && networkInfo.isConnectedOrConnecting()) {
                    return true;
                }
            }
        }
        else {
            @SuppressWarnings("deprecation") final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType);
            if (networkInfo.isConnectedOrConnecting()) {
                return true;
            }
        }
        return false;
    }
Lukas Hanacek
  • 1,364
  • 14
  • 25