Is there a way to find out if Data Saver is enabled?

Android 7.0 Nougat has added a data persistence feature that allows users to restrict the background data of certain apps (including push notifications). When Data Saver is enabled, only listed apps found in

Settings -> Data Warehouse -> Unlimited Data Access

allowed to receive push notifications and make background network calls. If Data Saver is turned off and your app is not on the unlimited list, it looks a lot like turning off push notifications.

There is a use case in my application where it is waiting for a push message.

I wonder if there is a way to find out if Data Saver is enabled and potentially if my app is in the Unlimited Data Access list to see if push notifications are enabled for my app and so if there is a point in pending tap and opportunity make any network calls while the app is in the background at a specific time.

+3


source to share


1 answer


To check if data saver is enabled and if your app is whitelisted, you can use 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 persistence 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 restrict data usage regardless of whether data saver is enabled or disabled or if your app is whitelisted.

Complete the example in the docs where you can also learn how to request whitelist permission and listen for changes in data saving settings.

+3


source







All Articles