Monday, January 5, 2015

Disable Camera on Android Devices

I have been going through forums and blogs where developers queried about disabling camera on android devices. Its pretty much easy. Here is how.

You need to use Device Admin privileges to have the rights to disable the camera. Remember that once an app is registered as Device Admin, it can only be uninstalled if the permissions from Device Admin (Settings section on your android device) is given.

1. Make a class (e.g. DevicePolicyActivity) and extend Activity
1.1 . Make another class  (e.g. AndroidDeviceAdminReceiver) and extend DeviceAdminReceiver

2. Make two variables like...

    DevicePolicyManager devicePolicyManager;
    ComponentName demoDeviceAdmin;

3. Initialize the variables above in onCREATE function like....
           devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

        demoDeviceAdmin = new ComponentName(this, AndroidDeviceAdminReceiver.class);

4. Now, add disable camera function against your button press or checkbox function as...

      devicePolicyManager.setCameraDisabled(demoDeviceAdmin, false);

5. Now, add Intent to your class like...

   private void activateDevicePolicyAdmin() {
        Intent intent = new Intent(
                DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                demoDeviceAdmin);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "Organization Policy ");
        startActivityForResult(intent, ACTIVATION_REQUEST);
    }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ACTIVATION_REQUEST:
                if (resultCode == Activity.RESULT_OK) {
                    Log.i(TAG, "Administration enabled!");
                } else {
                    Log.i(TAG, "Administration enable FAILED!");
                }
                return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

6. Put this in your Android Manifest file (if using Netbeans, its under Important Files section)

        <receiver
            android:name=".AndroidDeviceAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>

                <!-- This action is required -->
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>

            <!-- This is required this receiver to become device admin component. -->
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>

7. The XML file (device_admin.xml) must mention the privleges that you want to use...

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-policies>
               <disable-camera />
        </uses-policies>
</device-admin>


Here is your app for Disabling Camera in 7 easy steps. If you fail to understand any part, m round the corner!


No comments:

Post a Comment