6

I want to check if the user enabled background data on his/her device and display a message if it is disabled.

How can I check if it has been enabled? I tried

import android.provider.Settings;

//...
Settings.System.getString(getContentResolver(), Settings.Secure.BACKGROUND_DATA);
//and
Settings.Secure.getString(getContentResolver(), Settings.Secure.BACKGROUND_DATA);

But they are returning null.

Thank you, Achie.

achie
  • 4,716
  • 7
  • 45
  • 59

1 Answers1

10

You want to use the Connectivity Manager to get this info.

ConnectivityManager mgr = (ConnectivityManager)Context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean bgData = mgr.getBackgroundDataSetting();
Jess
  • 42,368
  • 6
  • 37
  • 51
  • 6
    Just a word of caution, this does not work as of ICS. In ICS this method will always return true. For the correct use in ICS see http://developer.android.com/reference/android/net/ConnectivityManager.html#getBackgroundDataSetting() – Gaurav Apr 08 '12 at 15:57
  • It's better to wrap it in `ConnectivityManagerCompat.getRestrictBackgroundStatus(mgr)` – Dmitry K Nov 25 '21 at 13:21