Android-利用设备管理器来实现锁屏功能并可一键自我卸载

来源:互联网 发布:d3.min.js 下载 编辑:程序博客网 时间:2024/05/22 07:48

设备管理器操作步骤

 

1,创建类DeviceAdminReceiver的子类

 

如:com.lmk.lockscreen.DeviceAdminSample(继承DeviceAdminReceiver类就ok!)


2,在清单文件中配置广播接收者

<receiver            android:name="com.lmk.lockscreen.DeviceAdminSample"            android:description="@string/sample_device_admin_description"            android:label="@string/sample_device_admin"            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>


3,配置字符串相关信息 

<string name="activity_sample_device_admin">设备管理员</string><string name="sample_device_admin_description">开启设备管理员,不开扣2000元</string><string name="sample_device_admin">管理员</string>


4,在res目录下创建xml文件夹,在该文件夹下创建device_admin_sample.xml文件,内容:

<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>

现在我们利用设备管理器来进行对手机的锁屏功能


1、先完成以上4个步骤;

2、以下为实现锁屏的核心代码:
<span style="font-size:14px;">public void lockScreen(View v) {// 没激活设备管理员就提醒ComponentName who = new ComponentName(this, DeviceAdminSample.class);DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);if(dpm.isAdminActive(who)){dpm.lockNow();finish();}else{//帮助打开激活设备管理器的界面// Launch the activity to have the user enable our admin.            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,                    "设备管理器。。。");            startActivityForResult(intent, 1);}}</span>




3、以下为实现自我卸载的核心代码:
<span style="font-size:14px;">public void RemoveApp(View v) {DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);ComponentName who=new ComponentName(this, DeviceAdminSample.class);dpm.removeActiveAdmin(who);// 调用卸载的页面,找卸载的意图Intent intent = new Intent();intent.setAction("android.intent.action.DELETE");intent.addCategory("android.intent.category.DEFAULT");intent.setData(Uri.parse("package:"+getPackageName()));startActivityForResult(intent, 0);}</span>




 

2 0
原创粉丝点击