Run intent DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN from a service

Posted by Zorb on Stack Overflow See other posts from Stack Overflow or by Zorb
Published on 2011-12-31T03:04:46Z Indexed on 2012/06/07 22:40 UTC
Read the original article Hit count: 246

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?

© Stack Overflow or respective owner

Related posts about android

Related posts about android-intent