Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different AndroidManifest files for different API levels

I am creating an app where I need to collect status bar notifications. Users are prompted to allow either my implementation of NotificationListenerService (API >= 18) or AccessibilityService (other devices) and they are redirected to settings screen.

When I am on API < 18 the user is redirected to Accessibility settings screen, he allows the Accessibility service and everything is OK. However, when the user is on 18>=, even if the user is redirected to Notification settings he still can navigate to Accessibility settings to allow also the Accessibility Service. Both of my services are then registering notifications and notifying me about that.

Obviously I can check from which service the message is coming and react accordingly but I would prefer some cleaner solution. I don't want the user to be able to allow both services (they both appear in settings).

Is there a way to do something like defining separate manifest files for different API levels or declare <uses-sdk> inside <application> tag so they will be used for different API levels? And of course, we cannot create services programmatically - we have to declare them in manifest.

like image 908
vanomart Avatar asked Mar 07 '16 23:03

vanomart


1 Answers

Step #1: Create a boolean resource in res/values/, named is18, set to false, and a second boolean resource named isLessThan18, set to true.

Step #2: Create a boolean resource in res/values-v18/, named is18, set to true, and a second boolean resource named isLessThan18, set to false.

Step #3: Use android:enabled="@boolean/is18" for your <service> element for your NotificationListenerService.

Step #4: Use android:enabled="@boolean/isLessThan18" for your <service> element for your AccessibilityService.

This will enable only one service per device, with the proper one dictated by the API level.

like image 98
CommonsWare Avatar answered Oct 13 '22 00:10

CommonsWare