Run intent DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN from a service
Asked Answered
S

3

6

I have a service and I want the service promote to enable it as Device Admin, until now I launched this kind of UI interactions from the service like

    Intent intent2 = new Intent();
    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent2.setAction(android.content.Intent.ACTION_VIEW);
    intent2.setDataAndType(uri, "application/vnd.android.package-archive");
    context.startActivity(intent2);

and it works, but with DevicePolicyManager I can't find the way:

        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,  "some text.");
        context.startActivity(intent);

does't work: do not promote nothing but also do not crash. Without intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); It simply crash because this code is inside a tread inside a service. Ideas?

Sugarcoat answered 31/12, 2011 at 3:4 Comment(1)
Hi, did you ever get an answer to this ?Artificiality
E
5

I've just fixed such issue for myself.

Note, that you need to put this code inside parent in Android Manifest.xml file:

    <receiver
        android:name=".ScreenLockerDeviceAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_policies" />

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

and it works :)

Elusion answered 7/6, 2012 at 22:36 Comment(2)
parent meaning the activity that calls the intent?Grandpapa
Finally, something that works!Montalvo
L
8

The reason is on the code of the Android DeviceAdminAdd class itself:

if ((getIntent().getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
            Log.w(TAG, "Cannot start ADD_DEVICE_ADMIN as a new task");
            finish();
            return;
       }

You should consider using another activity to call the DevicePolicyManager.

Legislatorial answered 25/6, 2013 at 12:44 Comment(2)
"You should consider using another activity to call the DevicePolicyManager." This is the only working solution for this question.Chaussure
What do you mean "call another activity?Midcourse
E
5

I've just fixed such issue for myself.

Note, that you need to put this code inside parent in Android Manifest.xml file:

    <receiver
        android:name=".ScreenLockerDeviceAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_policies" />

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

and it works :)

Elusion answered 7/6, 2012 at 22:36 Comment(2)
parent meaning the activity that calls the intent?Grandpapa
Finally, something that works!Montalvo
H
0

You don't even need to popup security setting's device admin UI. Here is a way to do it pragmatically:

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

where receiver needs to be define in manifest as described in android developer guide:
Device administration overview

Tested with android 6.0

David

Holstein answered 25/8, 2019 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.