现在三个类,小马就不一一解释,至于英语不好的朋友们,可以自行用工具查询下里面出现的单词,小马英语一般能用工具查到的,你照样可以,试下吧
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手机中手机屏幕的锁定方式有以下几种 ,不文字描述了,直接上图,看着爽
![](https://i-blog.csdnimg.cn/blog_migrate/0e098dacc1a790a0da7c11ff163343a9.jpeg)
得瑟一下,今天刚升的4.0.3,吼吼,,帅。。。
以下是小马拖的布局,因为只讲功能,界面就不说什么美观不美观了,拖三个按钮,三个按钮看按钮上的的文本内容就知道是用在什么地方的, 不多讲,今天小马先测试下系统锁,因为没学会走呢,就先不去学跑了,大伙跟着小马一起慢慢来,先从简单的开始吧。
界面效果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/1c2cccec3ac594a4fed6ca43e7af1482.jpeg)
点击第一个按钮会跳到权限提醒页面,如图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/dc782560e7afa526fc027abd25d6959e.jpeg)
上面这个页面只是提醒你你要激活的所有设备权限,点击激活后会跳回首页,再去点击系统锁使用时,就达到咱们的目的啦,吼吼。。如图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/51f52438a8e74bbfe4bd0ea5c08d694b.jpeg)
光看我做的效果可不行,大家别忘了在全局配置文件里面配置,不然报错的,添加以下代码:
- <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>
下面我贴出完整的代码,供朋友们学习使用,有不懂之处,可直接提出疑问,我们共同交流:
主类:
- 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;
-
-
-
-
-
-
-
-
-
- public class DevicePolicyManagerDemoActivity extends Activity {
-
- private Button startPermissionBtn;
- private Button stopPermissionBtn;
- private Button sysLockBtn;
-
- private DevicePolicyManager dpm;
- private ComponentName componentName ;
-
- @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 intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
-
-
-
- intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
-
-
-
- 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();
- }
- }
-
- }
接收类:
- 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 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 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>