Different AndroidManifest files for different API levels

℡╲_俬逩灬. 提交于 2019-12-23 15:37:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/35856163/different-androidmanifest-files-for-different-api-levels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!