5

Hello I want to start/activate Device Admin when application start without using Intent. Now I am using 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 dont want to use Intent. I want to start Device Admin directly .

Can anyone know how can I do that?

Thanks.

anddev
  • 3,144
  • 12
  • 39
  • 70
  • Why you do not wanted to use intent? we can not start any activity without intent – Dinesh Prajapati Nov 14 '11 at 10:00
  • Yes I know that but I dont want to start Activity I want to Enable Device Admin directly. For example, if I take ToggleButton then when I make ON then DeviceAdmin become Enable and OFF then Disable. Is it possible? – anddev Nov 14 '11 at 10:30
  • 1
    Device admin is very important for application so when application is in Device admin user can not uninstall it directly. hence you have to start the admin intent and has to use the Admin screen which Android is providing. :) toggling from the application is not possible AFAIK – Dinesh Prajapati Nov 14 '11 at 10:33
  • ok thanks. Can we get the status of device admin? I mean if device admin already enable then we installed our app so can we get the status of device admin in our app? – anddev Nov 14 '11 at 10:42
  • 1
    Yes we can get the Device Admin status. you need extend you r class with DeviceAdminReceiver and override OnEnabled/onDisabled method. else you can maintain the flag in preference for offline status. – Dinesh Prajapati Nov 14 '11 at 10:45
  • Yes I used that receiver. But I want the status when application start. Not when that particular class called OR when receiver called. Is it possible? – anddev Nov 14 '11 at 11:21
  • Maintain the flag in the preference when you enable it and use it when next time your application starts. and vice versa – Dinesh Prajapati Nov 14 '11 at 11:22
  • http://developer.android.com/guide/topics/admin/device-admin.html – Derzu Feb 26 '13 at 19:41

3 Answers3

4

Fortunately it's not possible without user interaction. User must confirm your app as device admin in settings so you can navigate user to the settings at best.

It would be a huge security risk if apps could activate device administrators without user interaction (device admin has pretty big privileges).

stevo.mit
  • 4,681
  • 4
  • 37
  • 47
-2

It's doable through the following ways:
1) Using add shell command:

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

2) Programmatically:

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

Tested with Android 6.0. No root needed.

David

us_david
  • 4,431
  • 35
  • 29
-2
Intent intent = new Intent(DevicePolicyManager. ACTION_ADD_DEVICE_ADMIN);
ComponentName compName = new ComponentName(getApplicationContext(), MyAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Bấm zô nút đồng ý(ACTIVE á)");
startActivityForResult(intent, RESULT_ENABLE);

=> I can grant permissions to android 5.1 but can't do it with version 6.0 and above

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
MrNgoan
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. Is this a question or an answer? If it is an answer it does not answer the question. To improve your answers, please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). – ChristianB Jan 04 '21 at 12:02