【问题标题】:Is there a system broadcast for when a user toggles the "Background restriction" for an app in Android?当用户在 Android 中切换应用的“背景限制”时,是否有系统广播?
【发布时间】:2019-05-21 18:14:46
【问题描述】:

要在 Android 手机 (Oreo+) 中切换“背景限制”,用户可以访问: 应用设置 -> 高级 -> 电池 -> 背景限制。

如果禁用,应用在后台时将无法再运行后台服务。这将节省电池电量。

但是,当应用返回前台时,这会导致崩溃。

我在 Android 文档 https://developer.android.com/reference/android/content/Intent.html 中找不到用于监听此事件的意图广播操作

有谁知道我如何检测此权限更改?

【问题讨论】:

    标签: android android-8.0-oreo


    【解决方案1】:

    你可以使用这个 API https://developer.android.com/reference/android/app/ActivityManager#isBackgroundRestricted()

        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            Log.d("TEST", "onCreate: activityManager.isBackgroundRestricted() = " + activityManager.isBackgroundRestricted());
        }
    

    并向用户显示某种对话框,告诉他们需要将其改回

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多