1.在AndroidManifest.xml添加
java
<receiver
android:name=".MyAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN"
android:exported="true">
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
</receiver>
2.在xml目录下添加 device_admin.xml
java
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<!--锁屏 -->
<force-lock />
<!--限制密码类型-->
<limit-password />
<!-- 监控屏幕解锁尝试次数 -->
<watch-login />
<!-- 重置密码-->
<reset-password />
<!--恢复出厂设置-->
<wipe-data />
<!-- 设置锁定屏幕密码的有效期 -->
<expire-password />
<!-- 设置存储设备加密 -->
<encrypted-storage />
<!-- 停用相机 -->
<disable-camera />
</uses-policies>
</device-admin>
- 添加DeviceAdminReceiver.java
java
package xxxxx;
import android.content.Context;
import android.content.Intent;
public class DeviceAdminReceiver extends android.app.admin.DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
// 管理员权限已激活
}
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
// 管理员权限已被取消激活
}
}
- 添加MyAdminReceiver
java
package xxxx;
import android.content.Context;
import android.content.Intent;
public class MyAdminReceiver extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
// 当设备管理员被激活时触发
}
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
// 当设备管理员被取消激活时触发
}
}
- 在MainActivity.java 或者其他 Activity里添加
java
private DevicePolicyManager mDPM;
private ComponentName mAdminComponent;
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminComponent = new ComponentName(this, MyAdminReceiver.class);
if (!mDPM.isDeviceOwnerApp(getPackageName())) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminComponent);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "DEVICES");
startActivityForResult(intent, 100);
} else {
}
//显示方法 packageName包名
public void showPack(String packageName) {
try {
mDPM.setApplicationHidden(mAdminComponent, packageName, false);
} catch (Exception e) {
}
}
//隐藏方法 packageName包名
public void hidePack(String packageName) {
try {
mDPM.setApplicationHidden(mAdminComponent, packageName, true);
} catch (Exception e) {
}
}
//锁屏
public void lockPm() {
try {
mDPM.lockNow();
} catch (Exception e) {
}
}
//亮屏
public void wakeUpAndUnlock(Context context) {
//屏锁管理器
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
//获取电源管理器对象
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
@SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
//点亮屏幕
wl.acquire();
//释放
wl.release();
}
- 最重要的是如何开启 DeviceOwner
使用cmd,adb shell
输入 dpm set-device-owner 包名/.MyAdminReceiver
如果启用成功,就可以控制,如果不行,请联系硬件厂商。
如何解除
输入 dpm remove-active-admin 包名/.MyAdminReceiver
或者在代码上
mDPM.clearDeviceOwnerApp(this.getPackageName());