一、项目简介:
项目:《Android 一键锁屏》
开发周期:4天
代码量:100行
————————————————————————
二、项目流程:
001
|
三、项目代码
|
002
|
1 、主程序代码:
|
003
|
Java代码
|
004
|
private DevicePolicyManager
|
005
|
private ComponentName
|
006
|
|
007
|
@Override |
008
|
protected void onCreate(Bundle
|
009
|
super .onCreate(savedInstanceState);
|
010
|
setContentView(R.layout.locklayout);
|
011
|
|
012
|
//获取设备管理服务
|
013
|
policyManager
|
014
|
|
015
|
//AdminReceiver
|
016
|
componentName new ComponentName( this , class );
|
017
|
|
018
|
mylock();
|
019
|
//
|
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
|
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
|
//
|
041
|
Intent new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
|
042
|
|
043
|
//权限列表
|
044
|
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
|
045
|
|
046
|
//描述(additional
|
047
|
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "------ );
|
048
|
|
049
|
startActivityForResult(intent, 0 );
|
050
|
}
|
051
|
|
052
|
4 、AndroidManifest.xml
|
053
|
Java代码
|
054
|
<?xml "1.0" encoding= "utf-8" ?>
|
055
|
<manifest "http://schemas.android.com/apk/res/android" |
056
|
package = "cn.hnu" |
057
|
android:versionCode= "1" |
058
|
android:versionName= "1.0" >
|
059
|
<application "@drawable/icon" android:label= "@string/app_name" >
|
060
|
<activity ".LockFirst" |
061
|
android:label= "@string/app_name" >
|
062
|
<intent-filter>
|
063
|
<action "android.intent.action.MAIN" />
|
064
|
<category "android.intent.category.LAUNCHER" />
|
065
|
</intent-filter>
|
066
|
</activity>
|
067
|
<!--
|
068
|
<receiver ".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.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 "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)
时间: 2024-11-01 16:27:43