不同 API 级别的不同 AndroidManifest 文件 [英] Different AndroidManifest files for different API levels

查看:49
本文介绍了不同 API 级别的不同 AndroidManifest 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,我需要在其中收集状态栏通知.系统会提示用户允许我的 NotificationListenerService (API >= 18) 或 AccessibilityService(其他设备)的实现,并且他们将被重定向到设置屏幕.

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.

当我使用 API <18 用户被重定向到辅助功能设置屏幕,他允许辅助功能服务,一切正常.但是,当用户在 18>= 时,即使用户被重定向到通知设置,他仍然可以导航到辅助功能设置以允许辅助功能服务.然后我的两个服务都会注册通知并通知我.

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).

有没有办法做一些事情,比如为不同的 API 级别定义单独的清单文件或在 标签中声明 以便他们用于不同的 API 级别?当然,我们不能以编程方式创建服务 - 我们必须在清单中声明它们.

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:在 res/values/ 中创建一个布尔资源,命名为 is18,设置为false,以及名为 isLessThan18 的第二个布尔资源,设置为 true.

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

第 2 步:在 res/values-v18/ 中创建一个布尔资源,命名为 is18,设置为 true,然后一秒钟名为 isLessThan18 的布尔资源,设置为 false.

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.

第 3 步:将 android:enabled="@boolean/is18" 用于 元素,用于 NotificationListenerService.

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

第 4 步:将 android:enabled="@boolean/isLessThan18" 用于 元素,用于 AccessibilityService.

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

这将使每台设备仅启用一项服务,适当的服务由 API 级别决定.

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

这篇关于不同 API 级别的不同 AndroidManifest 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆