Android: Enable/Disable Camera programmatically

2020-06-26 23:36发布

问题:

I want to enable/disable camera programmatically in my application.

回答1:

You cannot do this from a regular app. You can do it from a device administrator, but the user has to explicitly enable it and it is only available on ICS. Here's the reference: http://developer.android.com/guide/topics/admin/device-admin.html



回答2:

The OP is asking for any ideas, I got one idea and it could possibly work even without using the API provided by ICS.

for lower API version than ICS, you can Start a service that will lock the Camera when the screen is On and Release it when the Screen is Off. This is to minimize the usage of battery.

Since, only one process can use the Camera at a time, other applications like the Camera App will not be able to use the Camera.

The challenge now is how to ensure that the Service Hogging the Camera is always Running and prevent it from being uninstalled by the user.

Well, just a thought ;)



回答3:

Yes, you can use DeviceAdministration class and control the camera source(block and unblock). This is a repo with an example.



回答4:

Yes you can disable or enable camera but your app must device admin enabled.Following enables or disable camera:-

devicePolicyManager.setCameraDisabled(compName, isCameraOn);


**isCameraOn** is boolean true or false