如何将Flutter应用锁定到Kiosk应用?

时间:2019-11-01 19:16:40

标签: android flutter

我最近开始将Android应用程序移至Flutter。我喜欢Flutter应用的编码方式。 2天后,Flutter应用程序具有与Android应用程序相同的功能。唯一缺少的功能是锁定。 Android应用程序在拥有所有权的设备上运行,并且是设备所有者。通过以下方法可以实现锁定:

    private void setDefaultCosuPolicies(boolean active) {
    // set user restrictions
    setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
    setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
    setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
    setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
    setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);

    // disable keyguard and status bar
    mDevicePolicyManager.setKeyguardDisabled(mAdminName, active);
    mDevicePolicyManager.setStatusBarDisabled(mAdminName, active);

    // enable STAY_ON_WHILE_PLUGGED_IN
    enableStayOnWhilePluggedIn(false);

    // set this Activity as a lock task package

    mDevicePolicyManager.setLockTaskPackages(mAdminName,
            active ? new String[]{getPackageName()} : new String[]{});

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
    intentFilter.addCategory(Intent.CATEGORY_HOME);
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);

    if (active) {
      // set Cosu activity as home intent receiver so that it is started
      // on reboot
      mDevicePolicyManager.addPersistentPreferredActivity(
              mAdminName, intentFilter, new ComponentName(
                      getPackageName(), MainActivity.class.getName()));
    } else {
      mDevicePolicyManager.clearPackagePersistentPreferredActivities(
              mAdminName, getPackageName());
    }
  }

  private void setUserRestriction(String restriction, boolean disallow) {
    if (disallow) {
      mDevicePolicyManager.addUserRestriction(mAdminName,
              restriction);
    } else {
      mDevicePolicyManager.clearUserRestriction(mAdminName,
              restriction);
    }
  }

  private void enableStayOnWhilePluggedIn(boolean enabled) {
    if (enabled) {
      mDevicePolicyManager.setGlobalSetting(
              mAdminName,
              Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
              Battery_PLUGGED_ANY);
    } else {
      mDevicePolicyManager.setGlobalSetting(
              mAdminName,
              Settings.Global.STAY_ON_WHILE_PLUGGED_IN, DONT_STAY_ON);
    }
  }

在MainActivitys onCreate函数中,调用setDefaultCosuPolicies(true)。我将这些功能复制到flutter应用程序中。添加了AdminReceiver并在AndroidManifest中配置了所有内容。第一次尝试没有用,因此我尝试通过MethodChannel调用setDefaultCosuPolicies函数。这也行不通。

有人知道如何激活信息亭功能吗?

预先感谢

0 个答案:

没有答案