java - 即使设备所有权设置为此应用,也允许使用 pin 取消固定应用
问题描述
我正在使用 dpm 命令 (dpm set-device-admin) 为我的应用程序设置设备所有权,并且在我的 MainActivity.java 中我放置了以下代码:
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// get this app package name
ComponentName mDPM = new ComponentName(this, MyAdmin.class);
if (myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {
// get this app package name
String[] packages = {this.getPackageName()};
// mDPM is the admin package, and allow the specified packages to lock task
myDevicePolicyManager.setLockTaskPackages(mDPM, packages);
startLockTask();
} else {
Toast.makeText(getApplicationContext(),"Unable to auto pin - not device owner", Toast.LENGTH_LONG).show();
}
每当我的应用程序启动时,它都会自动固定,使用户无法离开。在不设置设备所有权的情况下,我可以通过长按返回按钮并提供引脚来离开“固定”状态。
当我设置所有权时,此操作将被覆盖 - 我看到的只是“应用已固定:此设备上不允许取消固定”。有没有办法通过在没有设备所有权的情况下提供 pin 来启用取消固定?我仍然需要它来确保它在没有任何提示的情况下自动固定。
解决方案
可能的答案:
覆盖单/长按键,成功时请求 pin 和 unpin 应用程序
MainActivity.java
@Override
public void onBackPressed() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
Intent intent = km.createConfirmDeviceCredentialIntent(null, null);
if (intent != null) {
startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
// unpin the app
if (resultCode == RESULT_OK) {
stopLockTask();
} else {
// nope
Toast.makeText(this, "Pin is not correct", Toast.LENGTH_SHORT).show();
}
}
}
推荐阅读
- tensorflow - TensorFlow V1 的未来是什么?
- ruby - Jekyll 服务没有打开端口
- python - 为 DC GAN 创建图像数据集
- java - 如何从 POM.xml 中读取 application.properties
- android - 无法以编程方式删除android上的短信
- python - 获取邀请不和谐机器人 discord.py 的人
- javascript - 如何在引导程序 4 自定义选择中完全隐藏双箭头
- c - os161 中的 userptr_t 类型有什么用?
- javascript - 如何在父级底部使用 jQuery 或 css 修复元素
- javascript - 选中框时显示按钮