学习安卓设备管理器锁屏、清除数据总结—DevicePolicyManager

来源:互联网 发布:多益网络社招 编辑:程序博客网 时间:2024/05/21 22:52

一:设备管理器介绍:

Android 2.2 SDK提供了一个可管理和操作设备的API叫DevicePolicyManager,

使用这个API你可以接管手机的应用权限,

对手机做出很多大胆的操作,

比如锁屏、恢复出厂设置、设置密码、强制清除密码,修改密码、设置屏幕灯光渐暗时间间隔等操作。

二:设备管理器创建

1.要调用DevicePolicyManager 必须有个设备管理器admin,

那么第一步创建一个MyAdmin,必须继承DeviceAdminReceiver,

不用重写什么方法,


public class MyAdmin  extends DeviceAdminReceiver {<span style="white-space:pre"></span>//<span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, SimSun;">showToast方法方便查看方法调用情况并提示</span><span style="white-space:pre"></span>private void showToast(Context context, String msg) {               Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();    }       @Override
<span style="white-space:pre"></span><span style="background-color: rgb(51, 204, 255);"><span style="color:#3366ff;">/**</span></span>
<span style="white-space: pre; background-color: rgb(51, 204, 255);"><span style="color:#3366ff;"></span></span><pre name="code" class="html" style="font-size: 14px; line-height: 22px;"><span style="white-space:pre"></span>R.string.devices_onEnabled=""

<span style="background-color: rgb(51, 204, 255);"><span style="color:#3366ff;"><span style="white-space: pre;"></span>*/</span></span>    public void onEnabled(Context context, Intent intent) {        showToast(context, "<span style="color:#3333ff;">开启设备管理器</span>"<span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, SimSun;">);</span>    }    @Override    public CharSequence onDisableRequested(Context context, Intent intent) {        return "<span style="color: rgb(0, 0, 255); font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; line-height: 15.390625px; white-space: pre;">警告禁止用户的请求</span>";    }    @Override    public void onDisabled(Context context, Intent intent) {        showToast(context, "<span style="color:#3333ff;">设备管理器关闭</span>"));    }    @Override    public void onPasswordChanged(Context context, Intent intent) {        showToast(context, "<span style="color:#3333ff;">密码改变</span>"));    }}
2.这是个广播类,必须注册,

<pre name="code" class="html"><activity android:name=".app.DeviceAdminSample"            android:label="@string/activity_sample_device_admin">    <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.SAMPLE_CODE" />    </intent-filter></activity><receiver android:name=".app.DeviceAdminSample$DeviceAdminSampleReceiver"        android:label="@string/sample_device_admin"        android:description="@string/sample_device_admin_description"        android:permission="android.permission.BIND_DEVICE_ADMIN">    <meta-data android:name="android.app.device_admin"            android:resource="@xml/device_admin_sample" />    <intent-filter>        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />    </intent-filter></receiver>


解释:
Android:name
你的Admin全名
 Android:description
可以自定义的区域,"比如我是牛逼的设备管理器"

android:permission="android.permission.BIND_DEVICE_ADMIN"
权限,不必多说
 <pre name="code" class="html" style="font-size: 14px; line-height: 15.390625px;"><meta-data android:name="android.app.device_admin"            android:resource="@xml/device_admin_sample" />
设备管理器有哪些功能
res下建立一个XML文件夹,
下面建立一个.xml文件,配置设备管理器的功能<pre name="code" class="html" style="font-size: 14px; line-height: 15.390625px;">device_admin_sample.xml
</pre><pre>
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?><device-admin xmlns:android="http://schemas.android.com/apk/res/android">  <uses-policies>    <limit-password />    <watch-login />    <reset-password />    <force-lock />    <wipe-data />    <expire-password />    <encrypted-storage />    <disable-camera />  </uses-policies></device-admin>
这是所有权限,需要哪个留哪个即可
比如我要清除数据<pre name="code" class="java" style="font-size: 14px; line-height: 15.390625px;"> <wipe-data />
device_admin_sample.xml,配置文件.xml文件名称,随意定

3:现在你就能在手机中看到你定义的设备管理器了
设置--安全--设备管理器
4:设备管理器开启
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);                        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);                        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,                                mActivity.getString(R.string.add_admin_extra_app_text));                        startActivity(intent, REQUEST_CODE_ENABLE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,                                mActivity.getString(R.string.add_admin_extra_app_text));
第二个putExtra,value 可以自定义,放你放的文字吧!
注意:设备管理器开启之后才能执行相应功能



5.管理-设备管理器:

DevicePolicyManager

DevicePolicyManager mDPM =    (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Context.DEVICE_POLICY_SERVICE
是个常量


6:判断设备管理器是否开启方法:
ComponentName who=new ComponentName(context, MyAdmin.class);
//判断设备管理器状态
boolean isEnable=mDPM.isAdminActive(who);

7:清除数据

mDPM.wipeData(0);

8:锁屏

mDPM.lockNow();

0 0