0

I am launching DevicePolicyManager in an Activity after checking if user should be prompted to enable DeviceAdmin on device.

If user clicks "Activate button", deviceAdmin will be enabled.

If user clicks "Cancel" button, user will be shown a dialog that "You need to Activate Device Admin on device to proceed with the app". By clicking "OK" on this dialog, we exit the app.

This works as expected on any device. However, when "Do Not Keep Activities" is enabled on device from settings section, the above behaviour changes in case user chooses to "Cancel" the DeviceAdmin activation prompt. When user clicks Cancel on the prompt, the intent DevicePolicyManager is relaunched without showing the dialog -"You need to Activate Device Admin on device to proceed with the app". Does anybody know why this happens ?

android_learner
  • 343
  • 2
  • 13

2 Answers2

0

This class you would have written in your application, this will receive if you enable or disable DPM, so when you receive in onDisable then you can notify user,

public class DeviceAdmin extends DeviceAdminReceiver {

static SharedPreferences getSamplePreferences(Context context) {
    return context.getSharedPreferences(DeviceAdminReceiver.class.getName(), 0);
}

void showToast(Context context, CharSequence msg) {
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onEnabled(Context context, Intent intent) {
  //  showToast(context, "Device Admin: enabled");
}

@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
    //return "This is an optional message to warn the user about disabling.";
    return null;
}

@Override
public void onDisabled(Context context, Intent intent) {
  //  showToast(context, "Device Admin: disabled");
}

}

now coming to your point you will be starting DPM using some activity so in onActivityResult you receive the resultCode where you clicked cancel button or activate button using than code you can check if DPM was activated or cancelled if it was cancelled then notify user with alert and in alert give only one ok button and on click of that finish the activity i think this should work, if this doesn't work please let know with issue :) happy coding

Amit
  • 391
  • 3
  • 15
  • When DPM is started from my activity, I have added a check in onActivityResulty to get DPM status. There is no issue with this. But when I get the status that DPM was not activated, I show a dialog to user to notify DPM state. It works fine in normal case. In case "Do not keep Activities" is enabled then on click on Cancel on DPM prompt, the dialog alert does not show, but DPM prompt keeps showing . – android_learner Apr 12 '14 at 09:34
0

When "Do Not Keep Activities" is enabled : your main activity is finished as soon as the Device Admin activity is displayed.

When "Device Admin" is canceled, the previous activity is restored: onCreate is called with it's saved state as argument.

I guess you didn't save anything in this bundle and so your activity have the same behavior as if you launch it for the first time.

ben75
  • 29,217
  • 10
  • 88
  • 134
  • Thanks but I had tried that too... when Device Admin is cancelled, on Activity result I had saved the state of deviceadmin in bundle . In oncreate I checked if bundle had the value for deviceadmin state stored in variable . The variable had the expected value i.e. cancelled state of deviceadmin . So on retrieving this value then I tried showing the dialog. But the leak appears at this point. It seems the activity got restarted but was not yet ready to show the dialog In logcat I found this error :activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46029dd0 that wa – android_learner Apr 12 '14 at 09:59
  • http://stackoverflow.com/questions/11649949/how-to-know-dont-keep-activities-is-enabled-in-ics check this – Amit Apr 14 '14 at 06:00
  • and what do you receive in onActivityResult ? – Amit Apr 14 '14 at 06:02
  • if I cancel deviceadmin, I get resultCode != Activity.RESULT_OK – android_learner Apr 14 '14 at 15:10