为什么重新启动具有限制使用 UserManager.DISALLOW_USB_FILE_TRANSFER 的应用程序的设备会导致设备无法使用?

why restarting a device that has an application that restricts the use of UserManager.DISALLOW_USB_FILE_TRANSFER renders the device unusable?

我已经按照 android 开发人员 - Lock Task Mode 中的本指南创建了一个信息亭应用程序。只要设备完成启动,应用程序就可以自动启动,但问题是每当我 restart/shutdown 并启动具有此限制的设备时,设备将无法启动,它将卡在设备的品牌徽标屏幕中并且必须恢复出厂设置才能再次工作。

dpm.addUserRestriction(componentName, UserManager.DISALLOW_USB_FILE_TRANSFER);

如果我不重启设备,此限制工作正常,但有时设备需要关闭。如何在不破坏设备的情况下在启动期间正确设置此限制?

我找到了解决问题的方法。我已经为设备关闭创建了一个 BroadcastReceiver 并删除了限制并在设备重新启动时重新启用限制。


public class ShutDownReceiver extends BroadcastReceiver {

    private static final String TAG = "ShutDownReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (Intent.ACTION_SHUTDOWN.equals(action)){

            DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
            ComponentName cn = AdminReceiver.getComponentName(context);

            if (dpm != null && dpm.isDeviceOwnerApp(context.getPackageName())) {
                //This is a custom method
                setUserRestriction(dpm, cn, UserManager.DISALLOW_USB_FILE_TRANSFER, false);
            }
            Toast.makeText(context, "Shutting Down", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onReceive: ACTION_SHUTDOWN");
        }
    }
}

在清单中添加代码

        <receiver android:name=".receiver.ShutDownReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
                <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
            </intent-filter>
        </receiver>