device_policy_manager
一个 Flutter 插件,用于查询 Android 设备管理员 (DevicePolicyManager),特别是锁定屏幕
更多信息请查看 DevicePolicyManager
安装和使用
将包添加到您的 pubspec
dependencies:
device_policy_manager: any # or the latest version on Pub
在 AndroidManifest 中添加此项以将设备管理员接收器与您的应用程序绑定
...
<receiver android:name="device.policy.manager.DeviceAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="@xml/policies" />
<intent-filter android:exported="true">
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
<activity
.
.
.
</application>
在 res/xml 目录中创建 policies.xml,并在其中添加以下代码
<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
用法
/// Return `true` if the given administrator component is currently active (enabled) in the system.
final status = await DevicePolicyManager.isPermissionGranted();
/// request administrator permission
/// it will open the adminstartor permission page and return `true` once the permission granted.
/// An optional message providing additional explanation for why the admin is being added.
await DevicePolicyManager.requestPermession("Your app is requesting the Adminstration permission");
/// Remove administration permission from the current app.
await DevicePolicyManager.removeActiveAdmin();
/// Make the device lock immediately, as if the lock screen timeout has expired at the point of this call.
/// After this method is called, the device must be unlocked using strong authentication (PIN, pattern, or password).
await DevicePolicyManager.lockNow();
/// Determine whether or not the device's cameras have been disabled for this user.
final status = await DevicePolicyManager.isCameraDisabled();