Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically deactivate a Device Admininstrator in Android?

Is it possible to programmatically deactivate a (third-party) Device Administrator app?

I was able to retrieve all the apps with Device Administrator activated using the DevicePolicyManager and getActiveAdmins():

final DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
final List<ComponentName> adminList = dpm.getActiveAdmins();

for ( ComponentName app : adminList ) {
    Log.d(TAG, "App: ", app.getPackageName());
}

However, in order to deactivate them, I cannot use removeActiveAdmin(...), since it won't remove a component that is not my own app.

I was thinking to use an Intent and startActivity() in order to open the Device Administrator's deactivation page of that specific component I want to deactivate. Is it possible?

like image 232
Paolo Rovelli Avatar asked Apr 16 '26 00:04

Paolo Rovelli


1 Answers

actually, it is possible to go directly to the admin screen, but i'm not sure how safe it is, as the API itself isn't available and the paths might change over different android versions and roms.

here's what i've tested:

this will go directly to the activate/de-activate screen of the app you choose:

final Intent intent=new Intent();
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.DeviceAdminAdd"));
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,componentName);
activity.startActivity(intent);

this will go to the list of admin apps:

final Intent intent=new Intent();
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.DeviceAdminSettings"));
activity.startActivity(intent);

if anyone has a more official ,safer way to do it, please write it down.

those are quite risky, so you could first use this method:

Intent intent=new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
final PackageManager packageManager=context.getPackageManager();
final List<ResolveInfo> resolveInfos=packageManager.queryIntentActivities(intent,0);
if(resolveInfos!=null&&!resolveInfos.isEmpty())
  try
    {
    final ResolveInfo resolveInfo=resolveInfos.get(0);
    intent=new Intent();
    intent.setComponent(new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name));
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,componentNameResult);
    context.startActivity(intent);
    return true;
    }
  catch(final Exception e)
    {}

you can add try-catch for each of the methods, and if all failed , use:

final Intent intent=new Intent(Settings.ACTION_SECURITY_SETTINGS);
activity.startActivity(intent);
like image 188
android developer Avatar answered Apr 18 '26 13:04

android developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!