为什么重新启动具有限制 UserManager.DISALLOW_USB_FILE_TRANSFER

我按照 android 开发人员中的本指南创建了一个信息亭应用程序 -锁定任务模式。每当设备完成启动时,该应用程序就可以自动启动,但问题是每当我重新启动/关闭并启动具有此限制的设备时,设备将无法启动,它会卡在设备的品牌徽标屏幕中,并且具有恢复出厂设置即可再次工作。

dpm.addUserRestriction(componentName, UserManager.DISALLOW_USB_FILE_TRANSFER);

如果我不重新启动设备,则此限制工作正常,但在某些时候需要关闭设备。如何在启动过程中正确设置此限制而不破坏设备?


胡子哥哥
浏览 130回答 1
1回答

DIEA

我找到了解决该问题的解决方法。我创建了一个用于设备关闭的广播接收器,并在设备重新启动时删除限制并重新启用限制。public class ShutDownReceiver extends BroadcastReceiver {&nbsp; &nbsp; private static final String TAG = "ShutDownReceiver";&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; String action = intent.getAction();&nbsp; &nbsp; &nbsp; &nbsp; if (Intent.ACTION_SHUTDOWN.equals(action)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ComponentName cn = AdminReceiver.getComponentName(context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dpm != null && dpm.isDeviceOwnerApp(context.getPackageName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //This is a custom method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setUserRestriction(dpm, cn, UserManager.DISALLOW_USB_FILE_TRANSFER, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context, "Shutting Down", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "onReceive: ACTION_SHUTDOWN");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在清单中添加代码&nbsp; &nbsp; &nbsp; &nbsp; <receiver android:name=".receiver.ShutDownReceiver">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.ACTION_SHUTDOWN" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; </receiver>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java