How to know if data Saver is enabled on android phones

711 views Asked by At

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?

2

There are 2 answers

1
Ravi On

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.

1
Ravi On

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();
}