标签:
Android 2.2(API 8)开始提供了一个可管理和操作设备的类DevicePolicyManager
,通过这个类可以进行锁屏、设置密码、清除密码、恢复出厂设置等操作。具体功能有:
MyDeviceAdminReceiver.java
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyDeviceAdminReceiver extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
Toast.makeText(context, "设备管理器:已激活", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Toast.makeText(context, "设备管理器:未激活", Toast.LENGTH_SHORT).show();
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return "这是一个可选的消息,警告有关禁止用户的请求";
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
super.onPasswordChanged(context, intent);
Toast.makeText(context, "设备管理;密码已经改变", Toast.LENGTH_SHORT).show();
}
@Override
public void onPasswordFailed(Context context, Intent intent) {
super.onPasswordFailed(context, intent);
Toast.makeText(context, "设备管理;改变密码失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
super.onPasswordSucceeded(context, intent);
Toast.makeText(context, "设备管理;改变密码成功", Toast.LENGTH_SHORT).show();
}
}
my_device_admin.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<disable-camera /> <!-- 停用相机 -->
<disable-keyguard-features /> <!-- 锁屏时禁用某些功能 -->
<encrypted-storage /> <!-- 设置存储设备加密 -->
<expire-password /> <!-- 设置锁定屏幕密码的有效期 -->
<force-lock /> <!-- 锁定屏幕 -->
<limit-password /> <!-- 设置密码规则 -->
<reset-password /> <!-- 更改屏幕解锁密码 -->
<set-global-proxy /> <!-- 设置设备全局代理 -->
<watch-login /> <!-- 监控屏幕解锁尝试次数 -->
<wipe-data /> <!-- 恢复出厂设置 -->
</uses-policies>
</device-admin>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xd.devicepolicymanager" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyDeviceAdminReceiver"
android:label="@string/app_lable"
android:description="@string/app_description"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/my_device_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="激活设备管理器"
android:id="@+id/btn_active"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消激活设备管理器"
android:id="@+id/btn_cancel_active"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改密码为1234"
android:id="@+id/btn_change_password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消密码"
android:id="@+id/btn_cancel_password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="锁屏"
android:id="@+id/btn_lock"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置锁屏时间(5s)"
android:id="@+id/btn_lock_time"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消设置锁屏时间"
android:id="@+id/btn_cancel_lock_time"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="恢复出厂设置"
android:id="@+id/btn_wipe_data"/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
/**
* 激活组件的请求码
*/
private static final int REQUEST_CODE_ACTIVE_COMPONENT = 1;
/**
* 设备安全管理服务,2.2之前需要通过反射技术获取
*/
private DevicePolicyManager devicePolicyManager = null;
/**
* 对应自定义DeviceAdminReceiver的组件
*/
private ComponentName componentName = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(this, MyDeviceAdminReceiver.class);
/**
* 激活设备管理器
*/
findViewById(R.id.btn_active).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
Toast.makeText(MainActivity.this, "设备管理器已激活", Toast.LENGTH_SHORT).show();
} else {
// 打备管理器的激活窗口
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
// 指定需要激活的组件
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "(激活窗口中的描述信息)");
startActivityForResult(intent, REQUEST_CODE_ACTIVE_COMPONENT);
}
}
});
/**
* 取消激活设备管理器
*/
findViewById(R.id.btn_cancel_active).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
devicePolicyManager.removeActiveAdmin(componentName);
Toast.makeText(MainActivity.this, "将触发DeviceAdminReceiver.onDisabled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "设备管理器未激活", Toast.LENGTH_SHORT).show();
}
}
});
/**
* 修改锁屏密码
*/
findViewById(R.id.btn_change_password).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
devicePolicyManager.resetPassword("1234", 0);
Toast.makeText(MainActivity.this, "若发生改变,则将触发DeviceAdminReceiver.onPasswordChanged", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "设备管理器未激活", Toast.LENGTH_SHORT).show();
}
}
});
/**
* 取消锁屏密码
*/
findViewById(R.id.btn_cancel_password).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
devicePolicyManager.resetPassword("", 0);
Toast.makeText(MainActivity.this, "若发生改变,则将触发DeviceAdminReceiver.onPasswordChanged", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "设备管理器未激活", Toast.LENGTH_SHORT).show();
}
}
});
/**
* 锁屏
*/
findViewById(R.id.btn_lock).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
devicePolicyManager.lockNow();
} else {
Toast.makeText(MainActivity.this, "设备管理器未激活", Toast.LENGTH_SHORT).show();
}
}
});
/**
* 设置锁屏时间
*/
findViewById(R.id.btn_lock_time).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
devicePolicyManager.setMaximumTimeToLock(componentName, 5000);
Toast.makeText(MainActivity.this, "锁屏设置将不可用", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "设备管理器未激活", Toast.LENGTH_SHORT).show();
}
}
});
/**
* 取消锁屏时间
*/
findViewById(R.id.btn_cancel_lock_time).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
devicePolicyManager.setMaximumTimeToLock(componentName, Long.MAX_VALUE);
Toast.makeText(MainActivity.this, "锁屏设置恢复可用", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "设备管理器未激活", Toast.LENGTH_SHORT).show();
}
}
});
/**
* 恢复出厂设置
*/
findViewById(R.id.btn_wipe_data).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAdminActive()) {
// 模拟器上无效,真机上慎用
// devicePolicyManager.wipeData(0);
Toast.makeText(MainActivity.this, "源码已屏蔽,慎用", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "设备管理器未激活", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 判断该组件是否有系统管理员的权限(【系统设置-安全-设备管理器】中是否激活)
* @return
*/
private boolean isAdminActive() {
return devicePolicyManager.isAdminActive(componentName);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_ACTIVE_COMPONENT) {
// 激活组件的响应
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "用户手动取消激活", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "已触发DeviceAdminReceiver.onEnabled", Toast.LENGTH_SHORT).show();
}
}
}
}
Android Studio 项目下载:android_devicepolicymanager
标签:
原文地址:http://blog.csdn.net/doris_d/article/details/51499836