如何解决如何将*多个*前台服务类型分配给一个 Android 服务
android:foregroundServiceType
指定服务是满足特定用例的前台服务。例如,前台服务类型“位置”表示应用正在获取设备的当前位置,通常是为了继续用户发起的与设备位置相关的操作。
您可以为特定服务分配多种前台服务类型。
问题
如何在 AndroidManifest.xml
中指定多种类型?
当前状态
到目前为止,我声明前台服务使用 camera 类型:
<application>
<service
android:name=".BackgroundService"
android:exported="false"
android:foregroundServiceType="camera"
android:process=":camera_process" />
</application>
但由于我必须对相机拍摄的图像进行地理参考,因此我还必须声明 foregroundServiceType="location"
。
我可以根据要求在 Java 代码中轻松引用这两种类型here:
R.attr.foregroundServiceType 属性中与相机对应的常量。使用相机设备或录制视频。对于 targetSdkVersion Build.VERSION_CODES.R 及更高版本的应用,如果在 manifest 和 Service.startForeground(int,android.app.Notification,int 中未指定此类型,则前台服务将无法访问相机)。
通过使用这个标志:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIFICATION_ID,PlaceholderNotificationBuilder.build(this),FOREGROUND_SERVICE_TYPE_MANIFEST);
} else {
startForeground(NOTIFICATION_ID,PlaceholderNotificationBuilder.build(this));
}
但是如何在 foregroundServiceType
中同时声明两个 AndroidManifest.xml
?
解决方法
经过一番搜索,我在 another documentation site 上找到了提示:
<manifest>
...
<service ... android:foregroundServiceType="location|camera" />
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。