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

所属分类: 软件编程 / Android 阅读数: 571
收藏 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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多