Detection programmatically if "Limit background data" is enabled for an Android application

I was looking for a stack overflow and could not find an answer to determine this set of parameters for my application. Android Marshmallow has an option in the settings:

Settings → Data usage → My application → Toggle to “Limit background data of the application” that “disable background data on the cellular network”

I want to warn the user if it is installed in my application. How to determine if this is installed for my application. Any pointers appreciated.

+4
source share
2 answers

you can use getActiveNetworkInfo()

, getActiveNetworkInfo() .

Reference

FYI:

Android, getBackgroundDataSetting

. false, , .

API 14.

0

Android, Android 7.0 (API- 24)...

https://developer.android.com/training/basics/network-ops/data-saver.html

ConnectivityManager connMgr = (ConnectivityManager)
    getSystemService(Context.CONNECTIVITY_SERVICE);
// Checks if the device is on a metered network
if (connMgr.isActiveNetworkMetered()) {
  // Checks user’s Data Saver settings.
  switch (connMgr.getRestrictBackgroundStatus()) {
    case RESTRICT_BACKGROUND_STATUS_ENABLED:
    // Background data usage is blocked for this app. Wherever possible,
    // the app should also use less data in the foreground.

    case RESTRICT_BACKGROUND_STATUS_WHITELISTED:
    // The app is whitelisted. Wherever possible,
    // the app should use less data in the foreground and background.

    case RESTRICT_BACKGROUND_STATUS_DISABLED:
    // Data Saver is disabled. Since the device is connected to a
    // metered network, the app should use less data wherever possible.
  }
} else {
  // The device is not on a metered network.
  // Use data as required to perform syncs, downloads, and updates.
}

, BroadcastReceiver ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED Context.registerReceiver(). , , , ConnectivityManager.getRestrictBackgroundStatus().

. , Context.registerReceiver(). , , .

0

All Articles