如何将*多个*前台服务类型分配给一个 Android 服务

问题描述

Android documentation 状态

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" />

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...