Android编程实现一键锁屏的方法

所属分类: 软件编程 / Android 阅读数: 528
收藏 0 赞 0 分享

本文实例讲述了Android编程实现一键锁屏的方法。分享给大家供大家参考,具体如下:

这里要用到下面两个类:

DeviceAdminReceiver 设备管理组件。这个类提供了一个方便解释由系统发出的意图的动作。你的设备管理应用程序必须包含一个DeviceAdminReceiver的子类。本程序中,就代表一个手机上的设备管理器.

DevicePolicyManager 一个管理设备上规范的类。 大多数客户端必须声明一个用户当前已经启用的DeviceAdminReceiver。 这个DevicePolicyManager为一个或者多个DeviceAdminReceiver实例管理这些规范。

DevicePolicyManager 的实例有个方法叫lockNow可以直接锁定屏幕.但是在这之前,需要激活程序中的设备管理器.

下面是主类LockActivity

package com.iceman.test; 
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; 
public class LockActivity extends Activity { 
  private DevicePolicyManager policyManager; 
  private ComponentName componentName; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  
  } 
  public void LockScreen(View v){
    policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 
    componentName = new ComponentName(this, LockReceiver.class); 
    if (policyManager.isAdminActive(componentName)) {//判断是否有权限(激活了设备管理器) 
      policyManager.lockNow();// 直接锁屏 
      android.os.Process.killProcess(android.os.Process.myPid()); 
    }else{ 
      activeManager();//激活设备管理器获取权限 
    }   
  }
  // 解除绑定
  public void Bind(View v){
   if(componentName!=null){
    policyManager.removeActiveAdmin(componentName);
    activeManager();
   }
  }
  @Override 
  protected void onResume() {//重写此方法用来在第一次激活设备管理器之后锁定屏幕 
    if (policyManager!=null && policyManager.isAdminActive(componentName)) { 
      policyManager.lockNow(); 
      android.os.Process.killProcess(android.os.Process.myPid()); 
    } 
    super.onResume(); 
  } 
  private void activeManager() { 
    //使用隐式意图调用系统方法来激活指定的设备管理器 
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); 
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一键锁屏"); 
    startActivity(intent); 
  } 
} 

下面是设备管理器类LockReceiver,这是一个继承自DeviceAdminReceiver的类,可以接收到激活/接触激活的广播,进行下一步操作,本程序中,只是简单打印一下信息.

import android.app.admin.DeviceAdminReceiver; 
import android.content.Context; 
import android.content.Intent; 
public class LockReceiver extends DeviceAdminReceiver{ 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    super.onReceive(context, intent); 
    System.out.println("onreceiver"); 
  } 
  @Override 
  public void onEnabled(Context context, Intent intent) { 
    System.out.println("激活使用"); 
    super.onEnabled(context, intent); 
  } 
  @Override 
  public void onDisabled(Context context, Intent intent) { 
    System.out.println("取消激活"); 
    super.onDisabled(context, intent); 
  } 
} 

主配置文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.iceman.test" 
  android:versionCode="1" 
  android:versionName="1.0" > 
  <uses-sdk android:minSdkVersion="9" /> 
  <application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
      android:name=".LockActivity" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Translucent" > 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
    <receiver 
      android:name=".LockReceiver" 
      android:description="@string/app_name" 
      android:label="@string/app_name" 
      android:permission="android.permission.BIND_DEVICE_ADMIN" > 
      <meta-data 
        android:name="android.app.device_admin" 
        android:resource="@xml/lock_screen" /> 
      <intent-filter> 
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> 
      </intent-filter> 
    </receiver> 
  </application> 
</manifest> 

其中lock_screen是设备管理器的权限声明,需要在res/xml目录下以xml文件形式定义

<?xml version="1.0" encoding="UTF-8"?> 
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" > 
  <uses-policies> 
    <!-- 锁定屏幕 --> 
    <force-lock /> 
  </uses-policies> 
</device-admin> 

OK.现在自己也可以做一键锁屏了.不用去网上找各种各样带广告带推送的了.

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android实现信号强度监听的方法

这篇文章主要介绍了Android实现信号强度监听的方法,是Android手机中很常见的一个实用功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现Activity界面切换添加动画特效的方法

这篇文章主要介绍了Android实现Activity界面切换添加动画特效的方法,非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中Dialog去黑边的方法

这篇文章主要介绍了Android中Dialog去黑边的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt for Android开发实例教程

这篇文章主要介绍了Qt for Android开发的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期操作实例

这篇文章主要介绍了Android开发之时间日期操作,是Android程序开发中常见的一个功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期组件用法实例

这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之获取网络链接状态

这篇文章主要介绍了Android获取网络链接状态的方法,主要是通过ConnectivityManager类来完成的,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之广播机制浅析

这篇文章主要介绍了Android开发之广播机制浅析,主要包括了发布、接收及配置广播的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之登录验证实例教程

这篇文章主要介绍了Android开发之登录验证实现方法,包括发送数据、服务器端验证、配置文件等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之注册登录方法示例

这篇文章主要介绍了Android开发的注册登录方法,是针对Android程序设计中版本兼容性的进一步完善,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多