问题描述
android:foregroundServiceType
指定服务是满足特定用例的前台服务。例如,前台服务类型“位置”表示应用正在获取设备的当前位置,通常是为了继续用户发起的与设备位置相关的操作。
您可以为特定服务分配多种前台服务类型。
问题
如何在 AndroidManifest.xml
中指定多种类型?
当前状态
<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" />