Why UsageStats.mLaunchCount is not accessible in I

2019-05-31 01:09发布

问题:

I have all permissions in place and can get the list of apps, and their usage stats as a list of UsageStats instances. UsageStats has a public field called mLaunchCount, added on API 22 (based on git history of the file). Now I want to access this if the phone is running API 22+, but when I try to use it, the IDE complains Cannot resolve symbol mLaunchCount. If I try to access it via reflection, it works.

So basically this does not compile:

Log.d("test", "Count: " + usageStat.mLaunchCount);

While this does:

Field mLaunchCount = UsageStats.class.getDeclaredField("mLaunchCount");
int launchCount = (Integer)mLaunchCount.get(usageStat);
Log.d("Test", "Count: " + launchCount);

Any idea what's happening?

Thanks

回答1:

Because mLaunchCount is not part of the Android SDK. In the source code, it is marked with the @hide annotation, used for things that are public for Java reasons but are not part of the SDK.