Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

bucket - Android P: UsageStatsManager getAppStandbyBucket

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...