锁屏需要引入设备超级管理员。在文档Android开发文档的Administration中有详细的说明。Android设备管理系统功能和控制访问。
主要有一下几个步骤:
1 创建广播接收者,实现DeviceAdminReceiver
2 在清单文件中注册该广播(不同普通的广播,需按照说明格式):
- package com.andy.lockscreen;
- import android.app.admin.DeviceAdminReceiver;
- /**
- * @author Zhang,Tianyou
- * @version 2014年11月20日 下午9:51:42
- *
- * 特殊的广播接受者 接收 管理员权限广播
- */
- public class MyAdmin extends DeviceAdminReceiver{
- }
3 在res下创建xml文件夹,创建对应的xml文件device_admin_sample.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.andy.lockscreen"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <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=".MyAdmin"
- 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>
- </application>
- </manifest>
4 在values文件下string.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>
5 界面文件:
- <string name="sample_device_admin_description">用户管理员的描述信息</string>
- <string name="sample_device_admin">设置管理权限</string>
- <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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.andy.lockscreen.MainActivity" >
- <Button
- android:onClick="openAdmin"
- android:layout_alignParentTop="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="开启管理员权限" />
- <Button
- android:onClick="lockcreen"
- android:layout_centerInParent="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="一键锁屏" />
- <Button
- android:onClick="uninstall"
- android:layout_alignParentBottom="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="卸载锁屏" />
- </RelativeLayout>
- package com.andy.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 dpm;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
- }
- /**
- * 锁屏
- *
- * @param view
- */
- public void lockcreen(View view) {
- ComponentName who = new ComponentName(this, MyAdmin.class);
- // 判断是否已经开启管理员权限
- if (dpm.isAdminActive(who)) {
- // 锁屏
- dpm.lockNow();
- // 设置屏幕密码 第一个是密码 第二个是附加参数
- dpm.resetPassword("123", 0);
- // 清楚数据
- // WIPE_EXTERNAL_STORAGE 清楚sdcard的数据
- // 0 恢复出厂设置
- //dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
- } else {
- // 如果为未开启 提示
- Toast.makeText(MainActivity.this, "请先开启管理员权限!", Toast.LENGTH_SHORT)
- .show();
- }
- }
- /**
- * 代码开启管理权限
- *
- * @param view
- */
- public void openAdmin(View view) {
- // 创建一个Intent 添加设备管理员
- Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
- // 激活MyAdmin广播接收着
- ComponentName who = new ComponentName(this, MyAdmin.class);
- intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);
- // 说明用户开启管理员权限的好处
- intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
- "开启可以一键锁屏,防止勿碰");
- startActivity(intent);
- Toast.makeText(MainActivity.this, "管理员权限已开启!", Toast.LENGTH_SHORT).show();
- }
- /**
- * 卸载当前的软件 设备管理数据特殊应用 所以不能普通卸载
- */
- public void uninstall(View view) {
- // 1. 先清除管理员权限
- ComponentName who = new ComponentName(this,
- MyAdmin.class);
- dpm.removeActiveAdmin(who);
- // 2. 普通应用的卸载
- Intent intent = new Intent();
- intent.setAction("android.intent.action.VIEW");
- intent.addCategory("android.intent.category.DEFAULT");
- intent.setData(Uri.parse("package:"+getPackageName()));
- startActivity(intent);
- }
- }
来源: http://www.phpxs.com/code/1002183/