如何以编程方式限制Android中的背景数据?

时间:2016-09-07 07:41:22

标签: android performance android-studio

我正在尝试创建一个可以阻止个人应用程序使用数据的应用。我到处检查,一无所获。

我尝试了this,但它用于显示数据使用情况,但不是如何限制背景数据。

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.
}

1 个答案:

答案 0 :(得分:0)

您不能以编程方式执行此操作。没有应用程序应该能够禁用或启用数据使用,这将是一个安全问题。

您唯一能做的就是通过在系统设置中打开请求的页面来提示用户这样做。