0

I know there are a few questions similar to this, ConnectionManager.getRestrictBackgroundStatus() will give me whether background data is disabled for my app.

For my use case I want to know specifically if the Data Saver is enabled for all apps

settings->dataSaver->restrictBackgroundData

or specific app background data is disabled

app_Name->Info->Network->disable_backgroundData

ConnectionManager.getRestrictBackgroundStatus() will give me the same answer in both the cases, how can I know which particular setting is enabled?

Rajnish suryavanshi
  • 3,168
  • 2
  • 17
  • 23

2 Answers2

0

Checking if Data Saver is enabled and if your app is whitelisted is possible via ConnectivityManager.getRestrictBackgroundStatus()

public boolean checkBackgroundDataRestricted() {
  ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

  switch (connMgr.getRestrictBackgroundStatus()) {
    case RESTRICT_BACKGROUND_STATUS_ENABLED:
    // Background data usage and push notifications are blocked for this app
    return true;

    case RESTRICT_BACKGROUND_STATUS_WHITELISTED: 
    case RESTRICT_BACKGROUND_STATUS_DISABLED:
    // Data Saver is disabled or the app is whitelisted  
    return false;
  }
}

If Data Saver is enabled and your app is not whitelisted, push notifications will only be delivered when your app is in the foreground.

You can also check ConnectivityManager.isActiveNetworkMetered() if you should limit data usage no matter if Data Saver is enabled or disabled or if your app is whitelisted.

Complete example in the docs where you can also learn how to request whitelist permission and listen to changes to Data Saver preferences.

Ravi
  • 2,277
  • 3
  • 22
  • 37
  • Hi Ravi, I think you are not getting my question, Please read it properly. I have already seen the answers you mentioned in other posts. To be clear about my question, an app background data can be disabled in two ways, one being data saver and other is by going into appInfo and disabling background data for that particular app, I want to know which Setting has been enabled. By connMgr.getRestrictBackgroundStatus() u won't get due to which setting the background data is disabled. – Harish Dadi Jan 29 '20 at 09:55
-1

Since Android Lollipop we have isPowerSaveMode() , here is the example-

PowerManager powerManager = (PowerManager)
    getActivity().getSystemService(Context.POWER_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
        && powerManager.isPowerSaveMode()) {
    // Animations are disabled in power save mode, so just show a toast instead.
    Toast.makeText(mContext, getString(R.string.toast), Toast.LENGTH_SHORT).show();
}
Ravi
  • 2,277
  • 3
  • 22
  • 37