从服务运行意图 DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN

发布于 2024-12-23 16:41:47 字数 875 浏览 2 评论 0原文

我有一项服务,我希望服务升级以将其启用为设备管理员, 到目前为止,我从服务中启动了这种 UI 交互

    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);

,并且它可以工作,但是使用 DevicePolicyManager 我找不到方法:

        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);

不起作用:不促进任何内容,但也不会崩溃。如果没有 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 它只会崩溃,因为此代码位于服务内的线程内。有想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅语花开 2024-12-30 16:41:47

原因在于 Android DeviceAdminAdd 类本身的代码:

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

您应该考虑使用另一个活动来调用 DevicePolicyManager。

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.

—━☆沉默づ 2024-12-30 16:41:47

我刚刚为自己解决了这个问题。

请注意,您需要将此代码放入 Android Manifest.xml 文件的父级中:

    <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>

并且它可以工作:)

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 :)

单挑你×的.吻 2024-12-30 16:41:47

您甚至不需要弹出安全设置的设备管理 UI。这是一种务实的方法:

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

需要在清单中定义接收器,如 Android 开发人员指南中所述:
设备管理概述

使用 Android 6.0

David进行了测试

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文