繁体   English   中英

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

[英]How to assign *multiple* foreground service types to one Android service

Android 文档状态

android:前台服务类型

指定该服务是满足特定用例的前台服务。 例如,“位置”的前台服务类型表示应用正在获取设备的当前位置,通常是为了继续用户发起的与设备位置相关的操作。

您可以将多个前台服务类型分配给特定服务。

问题

如何在AndroidManifest.xml中指定多种类型?

当前 state

到目前为止,我声明前台服务使用相机类型:

<application>
  <service
      android:name=".BackgroundService"
      android:exported="false"
      android:foregroundServiceType="camera"
      android:process=":camera_process" />
</application>

但由于我必须对相机拍摄的图像进行地理参考,我还必须声明foregroundServiceType="location"

我可以按照这里的要求在Java 代码中轻松引用这两种类型

R.attr.foregroundServiceType 属性中相机对应的常量。 使用相机设备或录制视频。 对于具有 targetSdkVersion Build.VERSION_CODES.R 及以上版本的应用,如果清单和 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));
}

但是如何在AndroidManifest.xml中声明两个foregroundServiceType

经过一番搜索,我在另一个文档站点上找到了提示:

<manifest>
...
<service ... android:foregroundServiceType="location|camera" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM