Different AndroidManifest files for different API levels
Asked Answered
F

1

8

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.

Falstaffian answered 7/3, 2016 at 23:25 Comment(0)
P
11

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.

Proliferation answered 7/3, 2016 at 23:30 Comment(3)
Is it possible to define something else than a bool? Specifically, something like launchMode?Anhanhalt
@RominaV: I'm not sure. The values for android:launchMode come from an enumerated list. I think they get mapped to integers. You might be able to define integer resources and use those, if you can find out the right integer values to use.Proliferation
@CommonsWare: I just did a similar thing with android:screenOrientation and it worked. Setting that parameter to a string value like fullUser broke the app installation process, but setting the parameter to an integer value (13 for fullUser) worked.Transmarine

© 2022 - 2024 — McMap. All rights reserved.