简体   繁体   English

如果为应用授予 START_ACTIVITIES_FROM_BACKGROUND 权限,则从前台服务启动相机(Android 11+)

[英]Start camera from foreground service if START_ACTIVITIES_FROM_BACKGROUND granted for the app (Android 11+)

My app process starts a foreground service when it receives BOOT_COMPLETED action我的应用程序进程在收到BOOT_COMPLETED操作时启动前台服务

That service is for video recording (dashboard camera app) which uses camera and microphone.该服务用于使用摄像头和麦克风的视频录制(仪表板摄像头应用程序)。

To start a camera from a foreground service on Android 11+ one of the conditions must be met (Exemptions from the restrictions to starting a camera/micro from a foreground service) https://developer.android.com/guide/components/foreground-services#bg-access-restriction-exemptions要从Android 11+ 上的前台服务启动摄像头,必须满足其中一个条件(免除从前台服务启动摄像头/微型的限制) -services#bg-access-restriction-exemptions

The only exception I can use is:我可以使用的唯一例外是:

The service is started by an app that has the START_ACTIVITIES_FROM_BACKGROUND privileged permission.该服务由具有 START_ACTIVITIES_FROM_BACKGROUND 特权的应用程序启动。

This one doesn't require any interactions from a user, a user just wants a video recording to be started automatically when he starts driving his car, he doesn't want manually make app visible or start recording from a notification, he doesn't care about all of this, which is ok, it's 2021 and such things can be done automatically, but thanks Google for such restrictions in 2021, yes, we should care about safety of private personal data of a user, but they just could add BACKGROUND_CAMERA , BACKGROUND_RECORD_AUDIO permissions with manual review on Google Play Console like they did with ACCESS_BACKGROUND_LOCATION这个不需要用户进行任何交互,用户只希望在他开始驾驶汽车时自动开始视频录制,他不希望手动使应用程序可见或从通知开始录制,他不关心这一切,没关系,现在是 2021 年,这些事情可以自动完成,但是感谢 Google 在 2021 年的这些限制,是的,我们应该关心用户私人个人数据的安全,但他们可以添加BACKGROUND_CAMERABACKGROUND_RECORD_AUDIO权限,在 Google Play 控制台上进行人工审核,就像他们对ACCESS_BACKGROUND_LOCATION所做的那样

If I understood correctly to get START_ACTIVITIES_FROM_BACKGROUND permission I must have one of the conditions be met (Exemptions from the restrictions to starting activities from the background) https://developer.android.com/guide/components/activities/background-starts#exceptions如果我理解正确以获得START_ACTIVITIES_FROM_BACKGROUND许可我必须满足其中一个条件(免除从后台启动活动的限制)

So I chose SYSTEM_ALERT_WINDOW permission, because if was granted by a user before then no interactions from the user is required and app can start an activity from background.所以我选择了SYSTEM_ALERT_WINDOW权限,因为如果之前由用户授予,则不需要用户进行任何交互,并且应用程序可以从后台启动活动。 This one can be granted with Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)这可以通过Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)授予

So do I have now START_ACTIVITIES_FROM_BACKGROUND permission granted or not?那么我现在是否授予了START_ACTIVITIES_FROM_BACKGROUND权限? I don't understand...我不明白...

If yes then I can use a camera in foreground service which was started from some background action如果是,那么我可以在前台服务中使用从某个后台操作启动的摄像头

But still但还是

fun isCameraAllowedForPackage(context: Context): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        val manager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
        val res = manager.unsafeCheckOpNoThrow(
            AppOpsManager.OPSTR_CAMERA,
            Process.myUid(),
            context.packageName
        )
         res == AppOpsManager.MODE_ALLOWED
    } else {
        true
    }
}

returns false for my Samsung S10 (Android 11) in foreground service before I try to use the camera在我尝试使用相机之前,在前台服务中为我的三星 S10(Android 11)返回false

Why?为什么?

If I make app visible at least once (start an activity f the app) then foreground service can start the camera (this method returns true ) even if the screen of the phone is turned off如果我使应用程序至少可见一次(启动应用程序的活动),那么即使手机屏幕关闭,前台服务也可以启动相机(此方法返回true

I also tried to use Pixel Emulator (Android 11) and there is different behavour for this one - app can start using the camera by the foreground service which was started in background even without SYSTEM_ALERT_WINDOW permission.我还尝试使用 Pixel Emulator (Android 11) 并且对此有不同的行为 - 即使没有SYSTEM_ALERT_WINDOW权限,应用程序也可以通过在后台启动的前台服务开始使用相机。 So I don't need any of that https://developer.android.com/guide/components/activities/background-starts#exceptions所以我不需要任何https://developer.android.com/guide/components/activities/background-starts#exceptions

I don't get it.我不明白。 Basically an Emulator doesn't follow those rules.基本上模拟器不遵循这些规则。

More about problems with my dashcam app有关我的行车记录仪应用程序问题的更多信息

Recently I was receving reports from users of my app with the next message最近,我收到了来自我的应用程序用户的报告,其中包含下一条消息

android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): validateClientPermissionsLocked:1426: Caller "MY_APP_PACKAGE_NAME" (PID 10300, UID 29860) cannot open camera "0" when sensor privacy is enabled android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): validateClientPermissionsLocked:1426: 调用者“MY_APP_PACKAGE_NAME”(PID 10300,UID 29860)在启用传感器隐私时无法打开摄像头“0”

or或者

Error happened during using Camera.使用相机时发生错误。 android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): connectHelper:2198: Camera "2" disabled by policy android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): connectHelper:2198: 相机“2”被政策禁用

or或者

android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): validateClientPermissionsLocked:1416: Caller "MY_APP_PACKAGE_NAME" (PID 10376, UID 25653) cannot open camera "0" from background (calling UID 10376 proc state 8) App catches such errors when it tries to open the camera android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): validateClientPermissionsLocked:1416: Caller "MY_APP_PACKAGE_NAME" (PID 10376, UID 25653) cannot open camera "0" from background (calling UID 10376 proc state 8) App catches such errors when it试图打开相机

A user can start recording manually from activity/notification/widget/floating window or recording can started automatically based on charging/Bluetooth/etc events (registered broadcast events within the foreground service for recording)用户可以从活动/通知/小部件/浮动 window 手动开始录制,也可以根据充电/蓝牙/等事件自动开始录制(在前台服务中注册的广播事件用于录制)

The foreground service WON'T start the recording if isCameraAllowedForPackage() returns false , no way, so it returns true in such cases and then app fails to open camera with CAMERA_DISABLED error, I don't get it.如果isCameraAllowedForPackage()返回false ,前台服务不会开始录制,不可能,所以在这种情况下它会返回true ,然后应用程序无法打开相机并CAMERA_DISABLED错误,我不明白。

I asked users about this issue, they answered it may happen some times (NOT ALWAYS, THEY DON'T SET ANY POLICY FOR THE CAMERA) when recording automatically starts/stops on charging/Bluetooth events.我向用户询问了这个问题,他们回答说在充电/蓝牙事件自动开始/停止录制时,有时可能会发生(不总是,他们不会为相机设置任何策略)。 So of course the app on their phones can usually open the camera, it's allowed to be used, by seems sometimes the stupid operation system prevents app from using the camera, and I don't understand why, there is no any info about this case in restrictions I mentioned before.所以当然他们手机上的应用程序通常可以打开相机,可以使用,有时愚蠢的操作系统会阻止应用程序使用相机,我不明白为什么,没有任何关于这个案例的信息在我之前提到的限制中。 So for users app was working for a while without issues, they opened the activity of the app at least once (on Samsung Android 11+ they have to do it), so the foreground service 100% had the permission to use the camera but still then after some time this error happened for them.因此,对于用户应用程序工作了一段时间没有问题,他们至少打开了一次应用程序的活动(在三星 Android 11+ 他们必须这样做),所以前台服务 100% 有权使用相机但仍然然后过了一段时间,这个错误发生在他们身上。

It's a different issue.这是一个不同的问题。 If it wasn't allowed for my app process then I would get such error如果我的应用程序进程不允许这样做,那么我会收到这样的错误

在此处输入图像描述

https://developer.android.com/guide/components/foreground-services#test-restrictions https://developer.android.com/guide/components/foreground-services#test-restrictions

Anyway I as I mentioned before I have a method to check if the camera is allowed to be used for my app process before I open it无论如何,正如我之前提到的,我有一种方法可以在打开相机之前检查是否允许将相机用于我的应用程序进程

There are so many problems with that.这有很多问题。 Phones (of different manufactures) behave differently. (不同制造商的)手机表现不同。 Some unexpected exceptions during using camera.使用相机时出现一些意外异常。

So basically even if everything was taken into account, for example, a user started the launch activity of the app, which starts the foreground service, in this case foreground is now allowed to use the camera, but still some issues with opening camera can happen while the same foreground service was running for some time所以基本上即使考虑了一切,例如,用户启动了应用程序的启动活动,该活动启动了前台服务,在这种情况下,现在允许前台使用相机,但打开相机仍然会出现一些问题当同一个前台服务运行了一段时间

So do I have now START_ACTIVITIES_FROM_BACKGROUND permission granted or not?那么我现在是否授予了 START_ACTIVITIES_FROM_BACKGROUND 权限?

No. That permission is defined in the framework as:否。该权限在框架中定义为:

    <permission android:name="android.permission.START_ACTIVITIES_FROM_BACKGROUND"
        android:protectionLevel="signature|privileged|vendorPrivileged|oem|verifier" />

To hold it, you need to request it in the manifest of your app, and your app needs to be part of the firmware build (or be installed on the privileged partition by a user of a rooted device).要保存它,您需要在应用程序的清单中请求它,并且您的应用程序需要是固件构建的一部分(或由 root 设备的用户安装在特权分区上)。

If I understood correctly to get START_ACTIVITIES_FROM_BACKGROUND permission I must have one of the conditions must be met (Exemptions from the restrictions to starting activities from the background) https://developer.android.com/guide/components/activities/background-starts#exceptions如果我理解正确才能获得START_ACTIVITIES_FROM_BACKGROUND许可,我必须满足其中一个条件(免除从后台启动活动的限制) 例外

That page does not refer to that permission.该页面未提及该权限。

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

相关问题 从 Android 11 后台启动的前台服务打开摄像头 - Open camera from foreground service which was started in background on Android 11 从android中的前台服务启动后台服务 O - Start background service from foreground service in android O Android Q - 从后台启动前台位置服务 - Android Q - Start foreground location service from the background 在后台启动前台摄像头录制服务 - Start foreground camera recording service in background 来自服务的Android Start Activity,无需启动应用程序的其他活动 - Android Start Activity from a service without starting the other activities of the app 在Android中启动Service并通过多个活动与之通信 - Start Service in Android and communicate with it from multiple Activities 检查应用程序是否可以在 Android 12+ 的后台启动前台服务 - Check if app can start foreground service in background on Android 12+ android阻止后台应用程序在前台启动活动 - android Prevent a background app from launching activities in the foreground android:当活动从服务开始时如何关闭最近的活动 - android: how to close recent activities when activities start from service 停止例行服务,将现有的后台进程带入前台 - Stop routine Service start from bringing an existing background process to foreground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM