由于此前在51写过屏幕锁相关的文章,在最近开发过程中也用到了屏幕锁,今天就抽个时间将屏幕锁的控制及实现代码复习及更一步学习,希望能帮助朋友们对屏幕锁更加了解,以备不时之用,九宫格 PIN密码 密码锁屏等的小马会有屏幕锁二中详细介绍,大家看来看这篇简单的,来热热身,先简单讲下下:
  之前在Android中,只能等到手机不使用规定时间或无操作时,手机会自动锁定,解锁用电源键对吧?现在好了,自从:API Level 8 (Android 2.2) 开始, Android提供了DevicePolicyManager类, 可以让你的应用程序也能执行屏幕锁定等操作,小马以下程序中会用到的锁定屏幕的操作类中要使用的对象有以下三个:
  现在三个类,小马就不一一解释,至于英语不好的朋友们,可以自行用工具查询下里面出现的单词,小马英语一般能用工具查到的,你照样可以,试下吧
  1.DevicePolicyManager
  Public interface for managing policies enforced on a device. Most clients
  of this class must  have published a DeviceAdminReceiver that the user
  has currently enabled.
  2.DeviceAdminReceiver
  Base class for implementing a device administration component. This class provides a
  convenience for interpreting the raw intent actions that are sent by the system.
  3.DeviceAdminInfo
  This class is used to specify meta information of a device administrator component.
  再不懂的可以在网上查下,以下是小马查到的,贴出来:
  DevicePolicyManager
  这是设备管理的主类。通过它可以实现屏幕锁定、屏幕亮度调节、出厂设置等功能。
  DeviceAdminReceiver
  该类继承自 BroadcastReceiver。 从源码可以看到,其实就是实现了一个OnReceive方法,该方法中根据不同的Action,执行相应的操作。 比如,如果激活成功,那么Action就是ACTION_DEVICE_ADMIN_ENABLED, 据此调用 onEnabled 方法。
  DeviceAdminInfo
  包括设备信息,Info,就是Information嘛,呵,,乱猜也可以猜到,至于里面什么属性,到时候朋友们可以直接”.”下就知道啦。
  比如:
  DeviceAdminReceiver.USES_POLICY_FORCE_LOCK , 这个就是本次要用的”强制锁定屏幕”的权限. 不过这些权限一般都直接通过XML文件来定义。 稍后你就会看到。
  Android手机中手机屏幕的锁定方式有以下几种 ,不文字描述了,直接上图,看着爽

  以下是小马拖的布局,因为只讲功能,界面就不说什么美观不美观了,拖三个按钮,三个按钮看按钮上的的文本内容就知道是用在什么地方的, 不多讲,今天小马先测试下系统锁,因为没学会走呢,就先不去学跑了,大伙跟着小马一起慢慢来,先从简单的开始吧。界面效果如下:

  点击第一个按钮会跳到权限提醒页面,如图所示:

  上面这个页面只是提醒你你要激活的所有设备权限,点击激活后会跳回首页,再去点击系统锁使用时,就达到咱们的目的啦,吼吼。。如图所示:

  光看我做的效果可不行,大家别忘了在全局配置文件里面配置,不然报错的,添加以下代码:
XML/HTML代码
    - <receiver  
-         android:name=".AdminReceiver"  
-         android:description="@string/description"  
-         android:label="@string/labelValue"  
-         android:permission="android.permission.BIND_DEVICE_ADMIN" >  
-               <meta-data  android:name="android.app.device_admin"  
-                           android:resource="@xml/lockourscreen"/>  
-          <intent-filter>  
-             <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />  
-          </intent-filter>  
- </receiver>  
  下面我贴出完整的代码,供朋友们学习使用,有不懂之处,可直接提出疑问,我们共同交流:
       主类:
  接收类:
Java代码
    - package com.xiaoma.www;   
-   
- import android.app.admin.DeviceAdminReceiver;  
- import android.app.admin.DevicePolicyManager;  
- import android.content.ComponentName;  
- import android.content.Context;  
- import android.content.Intent;  
-        import android.os.IBinder;  
- import android.util.Log;  
- import android.widget.Toast;  
- public class AdminReceiver extends DeviceAdminReceiver {  
-     @Override  
-     public DevicePolicyManager getManager(Context context) {  
-         Log.i("XiaoMaGuo", "调用了getManager()方法");  
-         return super.getManager(context);  
-     }  
-     @Override  
-    public ComponentName getWho(Context context) {  
-         Log.i("XiaoMaGuo", "调用了getWho()方法");  
-         return super.getWho(context);  
-     }   
-   
-      
-  
-   
-     @Override  
-     public void onDisabled(Context context, Intent intent) {  
-         Log.i("XiaoMaGuo", "调用了onDisabled()方法");  
-         Toast.makeText(context, "禁用设备管理", Toast.LENGTH_SHORT).show();   
-   
-        super.onDisabled(context, intent);  
-    }  
-     @Override  
-     public CharSequence onDisableRequested(Context context, Intent intent) {  
-         Log.i("XiaoMaGuo", "调用了onDisableRequested()方法");  
-         return super.onDisableRequested(context, intent);  
-     }   
-   
-      
-  
-   
-     @Override  
-     public void onEnabled(Context context, Intent intent) {  
-         Log.i("XiaoMaGuo", "调用了onEnabled()方法");  
-         Toast.makeText(context, "启动设备管理", Toast.LENGTH_SHORT).show();   
-   
-         super.onEnabled(context, intent);  
-     }  
-     @Override  
-     public void onPasswordChanged(Context context, Intent intent) {  
-         Log.i("XiaoMaGuo", "调用了onPasswordChanged()方法");  
-         super.onPasswordChanged(context, intent);  
-     }  
-     @Override  
-     public void onPasswordFailed(Context context, Intent intent) {  
-         Log.i("XiaoMaGuo", "调用了onPasswordFailed()方法");  
-         super.onPasswordFailed(context, intent);  
-     }  
-     @Override  
-     public void onPasswordSucceeded(Context context, Intent intent) {  
-         Log.i("XiaoMaGuo", "调用了onPasswordSucceeded()方法");  
-         super.onPasswordSucceeded(context, intent);  
-     }  
-     @Override  
-     public void onReceive(Context context, Intent intent) {  
-         Log.i("XiaoMaGuo", "调用了onReceive()方法");  
-         super.onReceive(context, intent);  
-     }  
-     @Override  
-     public IBinder peekService(Context myContext, Intent service) {  
-         Log.i("XiaoMaGuo", "调用了peekService()方法");  
-         return super.peekService(myContext, service);  
-     }   
-   
- }  
  权限文件lockourscreen.xml:
XML/HTML代码
    - <?xml version="1.0" encoding="UTF-8"?>  
-     <device-admin  
-       xmlns:android="http://schemas.android.com/apk/res/android">  
-         <uses-policies>  
-               
-             <force-lock />   
-    
-               
-                    
- .                <wipe-data />  
- .                  
- .                <reset-password />  
- .                  
- .                <limit-password />  
- .                  
- .                <watch-login />  
- .  
- .       </uses-policies>  
- .   </device-admin>  
  全局配置文件:
XML/HTML代码
    -     <?xml version="1.0" encoding="utf-8"?>  
-     <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
-         package="com.xiaoma.www"  
-         android:versionCode="1"  
-         android:versionName="1.0" >  
-         <uses-sdk android:minSdkVersion="15" />   
-    
-         <application  
-             android:icon="@drawable/ic_launcher"  
-             android:label="@string/app_name" >  
-             <activity  
-                 android:name="com.xiaoma.www.DevicePolicyManagerDemoActivity"  
-                 android:label="欢迎跟小马一起交流学习" >  
-                 <intent-filter>  
-                     <action android:name="android.intent.action.MAIN" />  
-                     <category android:name="android.intent.category.LAUNCHER" />  
-                 </intent-filter>  
-             </activity>   
-    
-             <receiver  
-                 android:name=".AdminReceiver"  
-                 android:description="@string/description"  
-                 android:label="@string/labelValue"  
-                 android:permission="android.permission.BIND_DEVICE_ADMIN"  
-                 >  
-                  <meta-data  
-                     android:name="android.app.device_admin"  
-                     android:resource="@xml/lockourscreen"/>  
- .               <intent-filter>  
- .                   <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />  
- .               </intent-filter>  
- .  
- .           </receiver>  
- .       </application>  
- .  
- .   </manifest>  
  目的只有一个,共同交流,取得进步,谢谢啦,吼吼,这篇只是提前热热身的,更详细的屏幕锁,还请继续关注屏幕锁定详解(二),希望能跟大家一起学习,一起加油,打气!!!加油加油!!!