问题
Our app runs as a background service, continuously getting the images from the frontal camera and then doing things with the images. However, when the user tries to open another app that uses the camera, two things happen: either the new app crashes and ours continues, or our crashes and the new one gets access to the camera.
Now, if our app loses control over the camera, there's an exception that we can catch, and we can then start trying to access it until we get access and things go back to normal.
The problem is when the other apps crash because of ours. Is there any way to detect this, so we can pause our app until we can access the camera again?
回答1:
This happens because camera is an exclusive resource.
I recommend you to read the Camera documentation, which says:
If your application does not properly release the camera, all subsequent attempts to access the camera, including those by your own application, will fail and may cause your or other applications to be shut down.
回答2:
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.
来源:https://stackoverflow.com/questions/38507185/detect-when-another-app-tries-to-use-the-camera