制作一个能一键锁屏的小应用。
1 创建一个工程- -
2 实例化设备策略管理者--DevicePolicyManager
3 创建一个类,继承DeviceAdminRecevider,他是一个广播接受者子类
4 清单文件注册这个广播子类,参考API源码,创建一个xml文件
5 使用设备策略管理者,一键锁屏lockNow 清除数据wipeData
6 代码开启设备管理者权限使用锁屏功能。
MainActivity
布局文件
1 创建一个工程- -
2 实例化设备策略管理者--DevicePolicyManager
3 创建一个类,继承DeviceAdminRecevider,他是一个广播接受者子类
4 清单文件注册这个广播子类,参考API源码,创建一个xml文件
5 使用设备策略管理者,一键锁屏lockNow 清除数据wipeData
6 代码开启设备管理者权限使用锁屏功能。
7 卸载锁屏应用
创建一个类,继承DeviceAdminRecevider,他是一个广播接受者子类
package com.itheima.lockscreen;
import android.app.admin.DeviceAdminReceiver;
public class MyAdmin extends DeviceAdminReceiver {
}
注册这个广播子类
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.lockscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.itheima.lockscreen.MyAdmin"
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>
</application>
</manifest>
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>
MainActivity
package com.itheima.lockscreen;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
//设备策略管理者
private DevicePolicyManager pdm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
}
/**
* 打开管理者权限
* @param view
*/
public void openAdmin(View view){
// Launch the activity to have the user enable our a d m in.
//创建Intent 添加一个设备管理员
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"开启了一键锁屏功能");
startActivity(intent);
}
/**
* 一键锁屏
* @param view
*/
public void lockScreen(View view){
ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
if(pdm.isAdminActive(mDeviceAdminSample)){
//锁屏
pdm.lockNow();
//设置解锁密码
pdm.resetPassword("", 0);
//恢复厂家设置
//pdm.wipeData(0);
//清空SD卡数据
//pdm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
}else{
Toast.makeText(this, "还没有打开设备管理员权限", Toast.LENGTH_SHORT).show();
return;
}
}
/**
* 卸载一键锁屏应用
* @param view
*/
public void uninstall(View view){
//移除管理员权限
ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
pdm.removeActiveAdmin(mDeviceAdminSample);
//普通应用卸载
Intent intent =new Intent();
intent.setAction("android.intent.action.UNINSTALL_PACKAGE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:"+getPackageName()));
startActivity(intent);
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.itheima.lockscreen.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="lockScreen"
android:text="一键锁屏" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openAdmin"
android:text="打开管理员权限" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="uninstall"
android:text="卸载该应用" />
</RelativeLayout>