2

I just wana share my code for making a device administrative app and asking a little help at the end of this. I have referred this link:

Device Administrator.

This is Manifest Code:

You have to declare a broadcast receiver in following way, attributes are self explanatory.

<receiver
        android:name=".DeviceAdmin"
        android:description="@string/data"
        android:label="Mobile Security"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/policies" />

        <intent-filter android:priority="1000">
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
</receiver>

android:resource="@xml/policies" this xml file contains the list of policies to use.

Now you need to declare a subclass of DeviceAdminReceiver like this which is used to register your app with device administrator of device:

public class DeviceAdmin extends DeviceAdminReceiver{

@Override
public void onDisabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onDisabled(context, intent);
    Toast.makeText(context, "App is UnLocked and can be uninstalled.",              Toast.LENGTH_SHORT).show();
}

@Override
public void onEnabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onEnabled(context, intent);
    Toast.makeText(context, "App is Locked and cannot be uninstalled.",        Toast.LENGTH_SHORT).show();
}           

}

the 2 methods onDesable() and onEnable() are just used to give message to user when he/she activates or deactivates the administration.

Now the activity where I am handing Locking and Unlocking the app:

btnLock.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ComponentName component = new ComponentName(StartActivity.this, DeviceAdmin.class);
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, component);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"Lock this app from being uninstalled");
            startActivity(intent);
            btnLock.setEnabled(false);
            btnUnlock.setEnabled(true);
        }
    });

    btnUnlock.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            DevicePolicyManager dPm = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
            ComponentName component = new ComponentName(StartActivity.this, DeviceAdmin.class);
            dPm.removeActiveAdmin(component);
            btnUnlock.setEnabled(false);
            btnLock.setEnabled(true);
        }
    });

I want to setup a password and that must be asked whenever my app will be deactivated from Device Administrator.

Caution Continues
  • 743
  • 2
  • 10
  • 25

0 Answers0