我最近开始将一个 Android 应用程序迁移到 Flutter。我喜欢 Flutter Apps 的编码方式。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 …Run Code Online (Sandbox Code Playgroud)