标签:
1,创建类DeviceAdminReceiver的子类
如:com.itydl.lockscreen.DeviceAdminSample
2,在清单文件中配置广播接收者
<receiver
android:name="com.itydl.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">管理员</string>
<string name="sample_device_admin_description">开启设备管理员,不开启扣2000块</string>
4,在res目录下创建xml文件夹,在该文件夹下创建deviceadminsample.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,在代码中创建设备管理器和组件
dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
ComponentName who = new ComponentName(this, DeviceAdminSample.class);
6,写功能
dpm.lockNow();一键锁屏
上述步骤有些晦涩,看一下实际操作步骤(建议通过阅读官网AP文档I一步步操作): <string name="activity_sample_device_admin">设备管理员</string>
<string name="sample_device_admin">管理员</string>
<string name="sample_device_admin_description">开启设备管理员,不开启扣2000块</string>
删除掉配置文件中系统自带的activity节点信息。修改复制过来的activity标签name为我们工程的活动名称。<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itydl.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="com.itydl.lockscreen.MainActivity" android:label="一键锁屏" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.itydl.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> </application> </manifest>
package com.itydl.lockscreen; import android.os.Bundle; import android.app.Activity; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Intent; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { private DevicePolicyManager dmp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dmp = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); } /** * 一键锁屏 * * @param v */ public void lockScreen(View v) { // 如果没有激活设备管理员,提醒用户做事 ComponentName who = new ComponentName(this, DeviceAdminSample.class); if (dmp.isAdminActive(who)) {// true表示已经激活 dmp.lockNow();// 一键锁屏 } 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, * mDeviceAdminSample); * intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, * mActivity.getString(R.string.add_admin_extra_app_text)); * startActivityForResult(intent, REQUEST_CODE_ENABLE_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); } } }
标签:
原文地址:http://blog.csdn.net/qq_32059827/article/details/51860900