Andrei Ciuca
Andrei Ciuca

Reputation: 185

getRunningTasks LOLLIPOP integration

getRunningTasks is deprecated in LOLLIPOP. You have to use UsageStatsManager in order to get the package name of the foreground app. I have tested on Android 6.0 and everything works.

The problem is that I do not want to set the project to run from Android 5.0(The most users are on KitKat). So I compile my project with LOLLIPOP and I run the functions that I need depending on the Android version(I make sure not to run any code with UsageStatsManager on a lower api lever). In my manifest I set minSdkVersion to 10. I have tested on Android 4.1.1 the application works but I see this in Logcat:

01-06 19:57:17.570: E/dalvikvm(31534): Could not find class 'android.app.usage.UsageStatsManager', referenced from method 
01-06 19:57:17.570: W/dalvikvm(31534): VFY: unable to resolve check-cast 39 (Landroid/app/usage/UsageStatsManager;) in 
01-06 19:57:17.570: D/dalvikvm(31534): VFY: replacing opcode 0x1f at 0x0009
01-06 19:57:17.585: D/dalvikvm(31534): DexOpt: unable to opt direct call 0x00a5 at 0x23 in L

Should I be worried about these Errors/Warnings? The app works but I am not sure if it would work on all devices(with api level less than 21).

Another possible solution is to use reflection and then you would get just some exceptions for the api level less than 21. I am not sure if this is worth the effort if the app is doing already what it must do(my only concern is from logcat ).

Upvotes: 1

Views: 260

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006664

Should I be worried about these Errors/Warnings?

No, so long as you are not trying to execute the lines that contain the missing items. There are usually lots of these messages for Android apps, as they come from standard "progressive enhancement/graceful degradation" programming. Dalvik/ART will try to resolve the symbols when the class is loaded, emitting the messages when symbols cannot be found. But, so long as you aren't using the missing symbols, life is good.

The app works but I am not sure if it would work on all devices(with api level less than 21).

If you are supporting Android 1.x, this approach will not work, as Dalvik would throw a real exception instead of just logging the missing symbols. If you are supporting Android 1.x in 2016, you are a saint, insane, or possibly both.

Upvotes: 1

Related Questions