从服务运行意图 DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN
我有一项服务,我希望服务升级以将其启用为设备管理员, 到目前为止,我从服务中启动了这种 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原因在于 Android DeviceAdminAdd 类本身的代码:
您应该考虑使用另一个活动来调用 DevicePolicyManager。
The reason is on the code of the Android DeviceAdminAdd class itself:
You should consider using another activity to call the DevicePolicyManager.
我刚刚为自己解决了这个问题。
请注意,您需要将此代码放入 Android Manifest.xml 文件的父级中:
并且它可以工作:)
I've just fixed such issue for myself.
Note, that you need to put this code inside parent in Android Manifest.xml file:
and it works :)
您甚至不需要弹出安全设置的设备管理 UI。这是一种务实的方法:
需要在清单中定义接收器,如 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:
where receiver needs to be define in manifest as described in android developer guide:
Device administration overview
Tested with android 6.0
David