1 回答
TA贡献1820条经验 获得超2个赞
我找到了解决该问题的解决方法。我创建了一个用于设备关闭的广播接收器,并在设备重新启动时删除限制并重新启用限制。
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>
添加回答
举报