【问题标题】:why restarting a device that has an application that restricts the use of UserManager.DISALLOW_USB_FILE_TRANSFER renders the device unusable?为什么重新启动具有限制使用 UserManager.DISALLOW_USB_FILE_TRANSFER 的应用程序的设备会导致设备无法使用?
【发布时间】:2020-01-04 17:32:24
【问题描述】:
我已经按照 android 开发者指南 - Lock Task Mode 创建了一个自助服务终端应用程序。应用程序可以在设备完成启动时自动启动,但问题是每当我重新启动/关闭并启动具有此限制的设备时,设备将无法启动它会卡在设备的品牌徽标屏幕中并且有将其恢复出厂设置以再次工作。
dpm.addUserRestriction(componentName, UserManager.DISALLOW_USB_FILE_TRANSFER);
如果我不重新启动设备但在某些时候需要关闭设备,则此限制可以正常工作。如何在不破坏设备的情况下在启动期间正确设置此限制?
【问题讨论】:
标签:
java
android
android-studio
android-6.0-marshmallow
kiosk-mode
【解决方案1】:
我找到了解决问题的方法。我已经为设备关闭创建了一个 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>