Android锁屏软件

【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 { 14private DevicePolicyManager policyManager; //管理和操作设备的API 15private ComponentName componentName; //打开其他应用程序中的Activity或服务 16 17@Override 18protected void onCreate(Bundle savedInstanceState) { 19super.onCreate(savedInstanceState); 20setContentView(R.layout.activity_main); 21 22//获取设备管理服务 23policyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); 24//AdminReceiver 继承自 DeviceAdminReceiver 25componentName = new ComponentName(this, AdminReceiver.class); 26 27//主方法 28myLock(); 29 30//killMyself ,锁屏之后就立即kill掉我们的Activity,避免资源的浪费 31android.os.Process.killProcess(android.os.Process.myPid()); 32} 33 34private void myLock(){ 35boolean isActive = policyManager.isAdminActive(componentName); 36if(!isActive){ 37//若没有权限就去获取权限 38activeManage(); 39 40return; 41//锁屏 42//policyManager.lockNow(); 43} 44else { 45policyManager.lockNow(); 46} 47} 48 49private void activeManage(){ 50//启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器 51Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 52//权限列表 53intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); 54//描述(additional explanation) 55intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "----Other Description----"); 56startActivityForResult(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 14public static SharedPreferences getDevicePreference(Context context){ 15//获取设备存储的数值 16return context.getSharedPreferences(DeviceAdminReceiver.class.getName(),0); 17} 18 19// 密码的特点 20public static String PREF_PASSWORD_QUALITY = "password_quality"; 21// 密码的长度 22public static String PREF_PASSWORD_LENGTH = "password_length"; 23 24public static String PREF_MAX_FAILED_PW = "max_failed_pw"; 25 26void showToast(Context context, CharSequence text) { 27Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); 28} 29 30@Override 31public void onEnabled(Context context, Intent intent) { 32// TODO Auto-generated method stub 33showToast(context, "设备管理:可用"); 34} 35 36@Override 37public void onDisabled(Context context, Intent intent) { 38// TODO Auto-generated method stub 39showToast(context, "Deactive successful!"); 40} 41 42@Override 43public CharSequence onDisableRequested(Context context, Intent intent) { 44// TODO Auto-generated method stub 45return "Are you sure want to Deactive?"; 46} 47 48@Override 49public void onPasswordChanged(Context context, Intent intent) { 50// TODO Auto-generated method stub 51showToast(context, "设备管理:密码己经改变"); 52} 53 54@Override 55public void onPasswordFailed(Context context, Intent intent) { 56// TODO Auto-generated method stub 57showToast(context, "设备管理:改变密码失败"); 58} 59 60@Override 61public void onPasswordSucceeded(Context context, Intent intent) { 62// TODO Auto-generated method stub 63showToast(context, "设备管理:改变密码成功"); 64} 65 }

AndroidManifest.xml
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.example.wangshibang.screenlock"> 4 5< application 6android:allowBackup="true" 7android:icon="@mipmap/lock" 8android:label="@string/app_name" 9android:supportsRtl="true" 10android:theme="@style/AppTheme"> 11< activity android:name=".Main" 12android:label="@string/app_name" 13android: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 21android:name=".AdminReceiver" 22android:label="ScreenLock" 23android:permission="android.permission.BIND_DEVICE_ADMIN"> 24< meta-data 25android:name="android.app.device_admin" 26android: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" 3android:orientation="vertical" android:layout_width="match_parent" 4android:layout_height="match_parent"> 5 6 < /LinearLayout>

楼主才疏学浅,不喜勿喷
最后附上源码的链接:链接:http://pan.baidu.com/s/1i5x5s6x 密码:es42

    推荐阅读