1

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

m.hashemian
  • 1,786
  • 2
  • 15
  • 31

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • You are right, it seems that's the reason. I had noticed the @ hide but thought that's to exclude it from Javadocs only, but it seems @ hide does more than that (http://stackoverflow.com/a/17056643/697716), and it's only accessible via reflection! – m.hashemian Nov 19 '15 at 00:38
  • @m.hashemian: More importantly, there is no guarantee that this field will exist on all relevant Android builds, including those from device manufacturers and custom ROM developers. – CommonsWare Nov 19 '15 at 00:39
  • True, has to be used considering it might not exist. – m.hashemian Nov 19 '15 at 00:40