首页 > 解决方案 > 即使设备所有权设置为此应用,也允许使用 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 来启用取消固定?我仍然需要它来确保它在没有任何提示的情况下自动固定。

标签: javaandroid

解决方案


可能的答案:

覆盖单/长按键,成功时请求 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();
   }
  }
 }

推荐阅读