Back to top

Now that you’ve set up the device admin receiver, activated your license, and configured the required permissions, you can use the APIs to toggle the camera state.

Create the camera policy

This method demonstrates how to use the Knox SDK to toggle the camera state.

   private void toggleCameraState() {
        // Instantiate the EnterpriseDeviceManager class
        EnterpriseDeviceManager enterpriseDeviceManager = EnterpriseDeviceManager.getInstance(this);
        // Get the RestrictionPolicy class where the setCameraState method lives
        RestrictionPolicy restrictionPolicy = enterpriseDeviceManager.getRestrictionPolicy();

        boolean isCameraEnabled = restrictionPolicy.isCameraEnabled(false);

        try {
            // Toggle the camera state on/off
            boolean result = restrictionPolicy.setCameraState(!isCameraEnabled);
            if(result) {
                mUtils.log(getResources().getString(R.string.camera_state, !isCameraEnabled));
            } else {
                mUtils.log(getResources().getString(R.string.camera_fail));
            }

        } catch (SecurityException e) {
            mUtils.processException(e, TAG);
        }
    }

Tutorial progress

You’ve completed 6/7 steps!

Next

Is this page helpful?