Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to know if Data Saver is enabled?

Android 7.0 Nougat added Data Saver feature allowing users to restrict background data of certain apps (including push notifications). When Data Saver is ON, only the apps on the list found in

Settings → Data Saver → Unrestricted data access

are allowed to receive push notifications and execute background network calls. If Data Saver is OFF and your app is not on the unrestricted list, it's pretty much like setting push notifications disabled.

There is a use case in my app where it's waiting for a push notification to come.

I wonder if there is a way to find out if Data Saver is enabled and potentially if my app is on the 'Unrestricted data access' list to know if push notifications are enabled for my app and therefore if there is a point in waiting for the push and a chance to execute any network calls while the app is in the background at a certain time.

like image 715
Marcel Bro Avatar asked Apr 13 '17 11:04

Marcel Bro


People also ask

Is it better to have data saver on or off?

Data Saver mode is useful but can also be an irritation if it limits apps too much. Instead of turning it on and specifying which apps should not be limited, you can decide which apps are using the most data and just limit those.

What does the data saver icon look like?

The Data Saver icon looks like an interrupted circle. In the Quick Settings, tap on Data Saver to enable the option. The icon changes colors and displays a plus sign in the middle to show that Data Saver mode is enabled.


1 Answers

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.

like image 69
Marcel Bro Avatar answered Oct 09 '22 01:10

Marcel Bro