admin管理员组

文章数量:1561817

由于此前在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手机中手机屏幕的锁定方式有以下几种 ,不文字描述了,直接上图,看着爽

 

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

 

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

 

 

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

 

光看我做的效果可不行,大家别忘了在全局配置文件里面配置,不然报错的,添加以下代码:

1 2 3 4 5 6 7 8 9 10 11 <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>

 

下面我贴出完整的代码,供朋友们学习使用,有不懂之处,可直接提出疑问,我们共同交流:

主类:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 package com.xiaoma.www;   import com.xiaoma.www.R;   import android.app.Activity; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;   /** * @Title: DevicePolicyManagerDemoActivity.java * @Package com.xiaoma.www * @Description: 屏幕锁测试 * @author MZH * @version V2.2 */ public class DevicePolicyManagerDemoActivity extends Activity {       private Button startPermissionBtn;     private Button stopPermissionBtn;     private Button sysLockBtn;       private DevicePolicyManager dpm;     private ComponentName componentName ;     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         //初始化         init();     }       /**      * 初始化实现      */     private void init(){         //资源声明定位         startPermissionBtn = (Button)findViewById(R.id.button1);         stopPermissionBtn = (Button)findViewById(R.id.button2);         sysLockBtn = (Button)findViewById(R.id.button3);           startPermissionBtn.setOnClickListener(listener);         stopPermissionBtn.setOnClickListener(listener);         sysLockBtn.setOnClickListener(listener);           //取得系统服务         dpm  = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);         componentName = new ComponentName(this, AdminReceiver.class);       }       /**      * 按钮监听器的实现,此处小马讲下,在实现监听的时候不要在一个分支里面写太多逻辑处理      * 分支只负责分支,不负责处理,要处理的话可以模块化,处理函数抽出去就可以了      * 小马希望大家从一开始就养成个好习惯,吼吼。。      */     private OnClickListener listener = new OnClickListener() {         @Override         public void onClick(View v) {             //以下是三个按钮分别处理的事件实现             switch (v.getId()) {             case R.id.button1:                 //开启设备权限                 startDeviceManager();                 break;             case R.id.button2:                 //停止设备权限                 stopDeviceManager();                 break;             case R.id.button3:                 //调用系统锁                 sysLock();                 break;             default:                 break;             }         }     };       /**      * 启动设备管理权限      */     private void startDeviceManager(){         //添加一个隐式意图,完成设备权限的添加         //这个Intent (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)跳转到 权限提醒页面              //并传递了两个参数EXTRA_DEVICE_ADMIN 、 EXTRA_ADD_EXPLANATION         Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);           //权限列表         //EXTRA_DEVICE_ADMIN参数中说明了用到哪些权限,         intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);           //描述(additional explanation)         //EXTRA_ADD_EXPLANATION参数为附加的说明         intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "--小马坏,小马爱,小马瓜瓜怪--");           startActivityForResult(intent, 0);           }       /**      * 禁用设备管理权限方法实现      */     private void stopDeviceManager(){         Log.i("XiaoMaGuo","------ unActiveManage ------");         boolean active = dpm.isAdminActive(componentName);         if (active) {             dpm.removeActiveAdmin(componentName);         }     }       /**      * 调用系统锁方法实现      */     private void sysLock(){         boolean active = dpm.isAdminActive(componentName);         if (active) {             dpm.lockNow();         }     }   }

 

接收类:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?xml version="1.0" encoding="UTF-8"?> <device-admin   xmlns:android="http://schemas.android/apk/res/android">     <uses-policies>         <!-- 强行锁定 -->         <force-lock />           <!--  因为今天我们只讲下简单的,所以其它的权限,小马先暂时注释掉咯,有兴趣的朋友们可以打开自己试下-->              <!-- 清除所有数据(恢复出厂设置)--> .              <wipe-data /> .              <!-- 重置密码 --> .              <reset-password /> .              <!-- 限制密码选择--> .              <limit-password /> .              <!-- 监控登录尝试 --> .              <watch-login /> . .     </uses-policies> . </device-admin>

全局配置文件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android/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>

目的只有一个,共同交流,取得进步,谢谢啦,吼吼,这篇只是提前热热身的,更详细的屏幕锁,还请继续关注屏幕锁定详解(二),希望能跟大家一起学习,一起加油,打气!!!加油加油!!!

 

  • 本文固定链接: http://www.xuanyusong/archives/1638
  • 转载请注明: 小马果 2012年08月13日 于 雨松MOMO程序研究院 发表

转载于:https://wwwblogs/lonng226/articles/4140978.html

本文标签: 二十三详解应用程序研究院屏幕