Android P: UsageStatsManager getAppStandbyBucket

2020-02-16 02:29发布

问题:

I want to display the current bucket of my application with a device under Android P beta. Therefore, I try to use the UsageStatsManager class like this :

@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()); 
        }
    }
}

My Manifest possesses the permission android.permission.PACKAGE_USAGE_STATS :

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

But I encounter a crash when I launch my application :

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)

It seems that I missed a permission, but even when I add the following permission :

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

I still have the same crash. I was thinking that maybe I miss the runtime call for the permission, but like the documentation stipulate :

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

It's not necessary. Even knowing that I tried to use the runtime permission, but I still get the crash. If anyone see what is missing, I would really appreciate the help.

回答1:

I think it's a bug. There is an open issue covering this in the issue tracker: https://issuetracker.google.com/issues/111102580.