检测其他应用何时尝试使用相机 [英] Detect when another app tries to use the camera

查看:63
本文介绍了检测其他应用何时尝试使用相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序作为后台服务运行,不断从前置摄像头获取图像,然后对图像进行处理.但是,当用户尝试打开另一个使用相机的应用程序时,会发生两件事:要么是新应用程序崩溃了,我们的继续运行,要么是我们崩溃了,而新的应用程序可以访问该摄像机.

现在,如果我们的应用失去了对摄像头的控制,我们可以捕捉到一个异常,然后我们就可以开始尝试访问它,直到获得访问权限并且一切恢复正常为止.

问题是其他应用由于我们的应用而崩溃时.有什么方法可以检测到这一点,因此我们可以暂停我们的应用程序,直到可以再次访问相机为止.

解决方案

不幸的是,Camera API或Android SDK通常不提供此类信息.好消息是,越来越多的新设备使您可以分别打开前置摄像头和后置摄像头.这样可以减少发生冲突的可能性.

根据您的描述,可能会使与您竞争的应用程序因访问相机而崩溃,这是合法的,但是,除了开玩笑之外,您无能为力.他们没有正确检查RuntimeException是他们的错.

Android最近加强了应用之间的隔离,因此可能无法跟踪启动了哪些应用(请参阅 解决方案

Unfortunately, Camera API or Android SDK in general don't provide this kind of info. The good news is that gradually, more new devices let you open the front-facing and the rear-facing cameras independently. This could reduce the probability of a clash.

From your description, it may be legitimate to crash the applications that compete with yours over access to camera, but, jokes aside, there is nothing you can do about it. It's their fault that they don't check the RuntimeException correctly.

Android has recently tightened the isolation between apps, so it may be impossible to track which applications are started (see App to monitor other apps on android), unless your app has system privileges.

这篇关于检测其他应用何时尝试使用相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆