Android P: UsageStatsManager getAppStandbyBucket

Android P: UsageStatsManager getAppStandbyBucket

我想在 Android P beta 下的设备上显示我的应用程序的 current bucket。因此,我尝试像这样使用 UsageStatsManager class :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (Build.VERSION.SDK_INT >= 28) {
        UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
        if (usageStatsManager != null) {
            Log.d(TAG, "getAppStandbyBucket():" + usageStatsManager.getAppStandbyBucket()); 
        }
    }
}

我的Manifest拥有权限android.permission.PACKAGE_USAGE_STATS :

<uses-permission 
    android:name="android.permission.PACKAGE_USAGE_STATS" 
    tools:ignore="ProtectedPermissions" />

但是我在启动我的应用程序时遇到了崩溃:

07-24 10:23:39.061 26967-26967/com.app.bucket E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.bucket, PID: 26967
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.bucket/com.app.bucket.MainActivity}: java.lang.SecurityException: MATCH_ANY_USER flag requires INTERACT_ACROSS_USERS permission at com.android.server.pm.PackageManagerService.getPackageUid:4343 com.android.server.pm.PackageManagerService$PackageManagerInternalImpl.getPackageUid:23964 com.android.server.usage.UsageStatsService$BinderService.getAppStandbyBucket:927 android.app.usage.IUsageStatsManager$Stub.onTransact:274 android.os.Binder.execTransact:731 : Neither user 10118 nor current process has android.permission.INTERACT_ACROSS_USERS.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.SecurityException: MATCH_ANY_USER flag requires INTERACT_ACROSS_USERS permission at com.android.server.pm.PackageManagerService.getPackageUid:4343 com.android.server.pm.PackageManagerService$PackageManagerInternalImpl.getPackageUid:23964 com.android.server.usage.UsageStatsService$BinderService.getAppStandbyBucket:927 android.app.usage.IUsageStatsManager$Stub.onTransact:274 android.os.Binder.execTransact:731 : Neither user 10118 nor current process has android.permission.INTERACT_ACROSS_USERS.
        at android.os.Parcel.createException(Parcel.java:1942)
        at android.os.Parcel.readException(Parcel.java:1910)
        at android.os.Parcel.readException(Parcel.java:1860)
        at android.app.usage.IUsageStatsManager$Stub$Proxy.getAppStandbyBucket(IUsageStatsManager.java:658)
        at android.app.usage.UsageStatsManager.getAppStandbyBucket(UsageStatsManager.java:479)
        at com.app.bucket.MainActivity.onCreate(MainActivity.java:60)

看来我错过了一个权限,但即使我添加了以下权限:

  <uses-permission
        android:name="android.permission.INTERACT_ACROSS_USERS"
        tools:ignore="ProtectedPermissions" />

我仍然遇到同样的崩溃。我在想,也许我错过了权限的运行时调用,但就像 documentation 规定的那样:

NOTE: Most methods on this API require the permission android.permission.PACKAGE_USAGE_STATS. However, declaring the permission implies intention to use the API and the user of the device still needs to grant permission through the Settings application. See Settings.ACTION_USAGE_ACCESS_SETTINGS. Methods which only return the information for the calling package do not require this permission. E.g. getAppStandbyBucket() and queryEventsForSelf(long, long).

没必要。即使知道我尝试使用运行时权限,但我仍然遇到崩溃。如果有人看到缺少的东西,我将非常感谢您的帮助。

我认为这是一个错误。在问题跟踪器中有一个未解决的问题:https://issuetracker.google.com/issues/111102580.