一、项目简介:
项目:《Android 一键锁屏》
开发周期:4天
代码量:100行
————————————————————————
二、项目流程:
001 |
三、项目代码 |
002 |
1、主程序代码: |
003 |
Java代码 |
004 |
private DevicePolicyManager policyManager; |
005 |
private ComponentName componentName; |
006 |
|
007 |
@Override |
008 |
protected void onCreate(Bundle savedInstanceState) { |
009 |
super.onCreate(savedInstanceState); |
010 |
setContentView(R.layout.locklayout); |
011 |
|
012 |
//获取设备管理服务 |
013 |
policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); |
014 |
|
015 |
//AdminReceiver 继承自 DeviceAdminReceiver |
016 |
componentName = new ComponentName(this, AdminReceiver.class); |
017 |
|
018 |
mylock(); |
019 |
// killMyself ,锁屏之后就立即kill掉我们的Activity,避免资源的浪费; |
020 |
android.os.Process.killProcess(android.os.Process.myPid()); |
021 |
|
022 |
} |
023 |
|
024 |
2、其中,mylock()为: |
025 |
Java代码 |
026 |
private void mylock(){ |
027 |
|
028 |
boolean active = policyManager.isAdminActive(componentName); |
029 |
if(!active){//若无权限 |
030 |
activeManage();//去获得权限 |
031 |
policyManager.lockNow();//并锁屏 |
032 |
} |
033 |
if (active) { |
034 |
policyManager.lockNow();//直接锁屏 |
035 |
} |
036 |
} |
037 |
3、activeManage()代码为: |
038 |
Java代码 |
039 |
private void activeManage() { |
040 |
// 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器 |
041 |
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); |
042 |
|
043 |
//权限列表 |
044 |
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); |
045 |
|
046 |
//描述(additional explanation) |
047 |
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "------ 其他描述 ------"); |
048 |
|
049 |
startActivityForResult(intent, 0); |
050 |
} |
051 |
|
052 |
4、AndroidManifest.xml |
053 |
Java代码 |
054 |
<?xml version="1.0" encoding="utf-8"?> |
055 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
056 |
package="cn.hnu" |
057 |
android:versionCode="1" |
058 |
android:versionName="1.0"> |
059 |
<application android:icon="@drawable/icon" android:label="@string/app_name"> |
060 |
<activity android:name=".LockFirst" |
061 |
android:label="@string/app_name"> |
062 |
<intent-filter> |
063 |
<action android:name="android.intent.action.MAIN" /> |
064 |
<category android:name="android.intent.category.LAUNCHER" /> |
065 |
</intent-filter> |
066 |
</activity> |
067 |
<!-- 设备管理 --> |
068 |
<receiver android:name=".AdminReceiver" |
069 |
android:label="@string/app_name" |
070 |
android:description="@string/app_name" |
071 |
android:permission="android.permission.BIND_DEVICE_ADMIN"> |
072 |
<meta-data android:name="android.app.device_admin" |
073 |
android:resource="@xml/lock_screen" /> |
074 |
<intent-filter> |
075 |
<action |
076 |
android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> |
077 |
</intent-filter> |
078 |
</receiver> |
079 |
</application> |
080 |
|
081 |
|
082 |
</manifest> |
083 |
5、其中lock_screen.xml(lock_screen.xml文件放在res/xml文件夹下)代码为: |
084 |
Java代码 |
085 |
<?xml version="1.0" encoding="UTF-8"?> |
086 |
<device-admin |
087 |
xmlns:android="http://schemas.android.com/apk/res/android"> |
088 |
<uses-policies> |
089 |
<!-- 强行锁定 在里仅这个是需要的--> |
090 |
<force-lock /> |
091 |
<!-- 清除所有数据(恢复出厂设置) --> |
092 |
<wipe-data /> |
093 |
<!-- 重置密码 --> |
094 |
<reset-password /> |
095 |
<!-- 限制密码选择 --> |
096 |
<limit-password /> |
097 |
<!-- 监控登录尝试 --> |
098 |
<watch-login /> |
099 |
</uses-policies> |
100 |
</device-admin> |
四、项目预览
五、附注
程序运行在android2.2以上平台(含2.2)


