How to activate DeviceAdmin when the application launches in Android?

Hello, I want to start / activate Device Admin when starting the application without using Intent. Now I use this code:

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, Global.mDeviceAdminSample); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Additional text explaining why this needs to be added."); startActivityForResult(intent, RESULT_ENABLE); 

But I do not want to use the intention. I want to start Device Admin directly.

Does anyone know how I can do this?

Thanks.

+4
source share
2 answers

Fortunately, it is not possible without user interaction . The user must confirm your application as the device settings in the settings so that you can best go to the user settings.

There would be a big security risk if applications could activate device administrators without interacting with the user (the device administrator has quite large privileges).

+1
source

This can be done in the following ways:
1) Using the add shell command:

 adb shell dpm set-device-admin --user 0 com.mydeviceadmin/.deviceAdminReceiver 

2) Software:

 Runtime.getRuntime().exec("dpm set-device-admin --user 0 com.mydeviceadmin/.deviceAdminReceiver") 

Tested with Android 6.0. No root needed.

David

-1
source

Source: https://habr.com/ru/post/1381090/


All Articles