5

I'm developing an application which runs as an Admin. I'm able to do it using the following code

DemoDeviceAdminReceiver.jav:

public class DemoDeviceAdminReceiver extends DeviceAdminReceiver {
    static final String TAG = "DemoDeviceAdminReceiver";

    /** Called when this application is approved to be a device administrator. */
    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);

        Toast.makeText(context, R.string.device_admin_enabled, Toast.LENGTH_LONG).show();
        Log.d(TAG, "onEnabled");
    }

    /** Called when disabling device administrator power. */
    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        Toast.makeText(context, R.string.device_admin_requesting_disable, Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDisableRequested");

        return super.onDisableRequested(context, intent);
    }

    /** Called when this application is no longer the device administrator. */
    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Toast.makeText(context, R.string.device_admin_disabled, Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDisabled");
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.d(TAG, "onPasswordChanged");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
        Log.d(TAG, "onPasswordFailed");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
        Log.d(TAG, "onPasswordSucceeded");
    }
}

Manifest:

<receiver
    android:name=".DemoDeviceAdminReceiver"
    android:permission="android.permission.BIND_DEVICE_ADMIN" >
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>

    <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/device_admin_sample" />
</receiver>

res/xml/device_admin_sample.xml:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
        <expire-password />
        <encrypted-storage />
    </uses-policies>
</device-admin>

Every thing works fine, but I don't know how to ask for a password whenever the user clicks on disable the app from admin rights.

Please help to solve this big riddle. Thanks

John Hascall
  • 9,176
  • 6
  • 48
  • 72
Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45
  • Hi chethan do u find any way for doing it – Kalai.G Jul 29 '14 at 17:24
  • @Kalai G so far no, if you find any, please let me know. Thanks – Chethan Shetty Jul 31 '14 at 12:22
  • So need the solution for this question. When user taps **Deactivate** button, I want to request user for admin credentials that will be verified from server. If admin user is valid only then admin control will be disabled. Please let me know if there has been any progress in this regard. – Abdul Rehman Oct 14 '15 at 05:50
  • @AbdulRehman I have stopped the R&D on the module. If you find any solutions please update with answer. – Chethan Shetty Oct 14 '15 at 09:02

1 Answers1

1

Couple of years late, but I just ran into the same problem and was able to solve it. Maybe the OP won't need the answer, but others - like me - might stumble over this in search for a solution.

Keep in mind that this only works if the user can NOT unlock the screen.

All you need to do is lock the screen in DeviceAdminReceiver's onDisableRequested method, so that the user will have to enter their password for unlocking.

Keep in mind that this method is intended to display a warning explaining why the user should not disable permissions. If null (the default from super) is returned, nothing is displayed and the permission is disabled anyway. So if we return any text, a dialog with that text as well as an ok and a cancel button will be displayed.

If you combine that with the screen lock like this:

public CharSequence onDisableRequested(Context context, Intent intent) {
    DevicePolicyManager deviceManger = (DevicePolicyManager)context.getSystemService(
            Context.DEVICE_POLICY_SERVICE);
    deviceManger.lockNow();
    return "Your warning";
}

the screen will turn off when the user tries to disable the permission, when they unlock the screen, they're required to enter their password. Once they did that they see a dialog with your warning, the cancel button and an ok button. Only if they then press Ok will the permission be disabled.

Syzygy
  • 578
  • 7
  • 27