首页 > 代码库 > Android锁屏软件

Android锁屏软件

需求描述:锁屏软件就是点击应用然后直接锁屏,不会弹出任何界面

设计思路:应用启动以后通过getSystemService获取系统的服务,调用安卓系统的DevicePolicyManager和ComponentName来对系统进行操作,先判断用户是否有相应的权限,如果有,直接锁屏,如果没有就先获取系统权限,再锁屏

废话不说,直接上代码

Main.java(主页面)

 1 package com.example.wangshibang.screenlock;
 2 
 3 import android.app.Activity;
 4 import android.app.admin.DevicePolicyManager;
 5 import android.content.ComponentName;
 6 import android.content.Context;
 7 import android.content.Intent;
 8 import android.os.Bundle;
 9 
10 /**
11  * Created by wangshibang on 2016-11-8.
12  */
13 public class Main extends Activity {
14     private DevicePolicyManager policyManager;//管理和操作设备的API
15     private ComponentName componentName;//打开其他应用程序中的Activity或服务
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         //获取设备管理服务
23         policyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
24         //AdminReceiver 继承自 DeviceAdminReceiver
25         componentName = new ComponentName(this, AdminReceiver.class);
26 
27         //主方法
28         myLock();
29 
30         //killMyself ,锁屏之后就立即kill掉我们的Activity,避免资源的浪费
31         android.os.Process.killProcess(android.os.Process.myPid());
32     }
33 
34     private void myLock(){
35         boolean isActive = policyManager.isAdminActive(componentName);
36         if(!isActive){
37             //若没有权限就去获取权限
38             activeManage();
39 
40             return;
41             //锁屏
42             //policyManager.lockNow();
43         }
44         else {
45             policyManager.lockNow();
46         }
47     }
48 
49     private void activeManage(){
50         //启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器
51         Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
52         //权限列表
53         intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
54         //描述(additional explanation)
55         intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "----Other Description----");
56         startActivityForResult(intent, 0);
57     }
58 }

AdminReceiver.java

 1 package com.example.wangshibang.screenlock;
 2 
 3 import android.app.admin.DeviceAdminReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.content.SharedPreferences;
 7 import android.widget.Toast;
 8 
 9 /**
10  * Created by wangshibang on 2016-11-8.
11  */
12 public class AdminReceiver extends DeviceAdminReceiver {
13 
14     public static SharedPreferences getDevicePreference(Context context){
15         //获取设备存储的数值
16         return context.getSharedPreferences(DeviceAdminReceiver.class.getName(),0);
17     }
18 
19     // 密码的特点
20     public static String PREF_PASSWORD_QUALITY = "password_quality";
21     // 密码的长度
22     public static String PREF_PASSWORD_LENGTH = "password_length";
23 
24     public static String PREF_MAX_FAILED_PW = "max_failed_pw";
25 
26     void showToast(Context context, CharSequence text) {
27         Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
28     }
29 
30     @Override
31     public void onEnabled(Context context, Intent intent) {
32         // TODO Auto-generated method stub
33         showToast(context, "设备管理:可用");
34     }
35 
36     @Override
37     public void onDisabled(Context context, Intent intent) {
38         // TODO Auto-generated method stub
39         showToast(context, "Deactive successful!");
40     }
41 
42     @Override
43     public CharSequence onDisableRequested(Context context, Intent intent) {
44         // TODO Auto-generated method stub
45         return "Are you sure want to Deactive?";
46     }
47 
48     @Override
49     public void onPasswordChanged(Context context, Intent intent) {
50         // TODO Auto-generated method stub
51         showToast(context, "设备管理:密码己经改变");
52     }
53 
54     @Override
55     public void onPasswordFailed(Context context, Intent intent) {
56         // TODO Auto-generated method stub
57         showToast(context, "设备管理:改变密码失败");
58     }
59 
60     @Override
61     public void onPasswordSucceeded(Context context, Intent intent) {
62         // TODO Auto-generated method stub
63         showToast(context, "设备管理:改变密码成功");
64     }
65 }

AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.wangshibang.screenlock">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/lock"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme">
11         <activity android:name=".Main"
12             android:label="@string/app_name"
13             android:theme="@android:style/Theme.NoDisplay">
14             <intent-filter>
15                 <action android:name="android.intent.action.MAIN" />
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19         <!-- 设备管理 -->
20         <receiver
21             android:name=".AdminReceiver"
22             android:label="ScreenLock"
23             android:permission="android.permission.BIND_DEVICE_ADMIN">
24             <meta-data
25                 android:name="android.app.device_admin"
26                 android:resource="@xml/lock_screen" />
27             <intent-filter>
28                 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
29             </intent-filter>
30         </receiver>
31     </application>
32 
33 </manifest>

res文件夹下建一个xml文件夹,里面有个lock_screen.xml文件

lock_screen.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <device-admin xmlns:android="http://schemas.android.com/apk/res/android">
 3     <uses-policies>
 4         <!-- 强行锁定  在里仅这个是需要的-->
 5         <force-lock />
 6         <!-- 清除所有数据(恢复出厂设置) -->
 7         <wipe-data />
 8         <!-- 重置密码 -->
 9         <reset-password />
10         <!-- 限制密码选择 -->
11         <limit-password />
12         <!-- 监控登录尝试 -->
13         <watch-login />
14     </uses-policies>
15 </device-admin>

最后是什么都没有的activity_main.xml文件

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3     android:orientation="vertical" android:layout_width="match_parent"
4     android:layout_height="match_parent">
5 
6 </LinearLayout>

楼主才疏学浅,不喜勿喷

最后附上源码的链接:链接:http://pan.baidu.com/s/1i5x5s6x 密码:es42

Android锁屏软件