首页 > 代码库 > BroadcastReceiver之实现锁屏、解锁样例

BroadcastReceiver之实现锁屏、解锁样例

好久没有写android的小样例了,因为前几天写了一篇关于Intent.Action的文章(http://blog.csdn.net/ljphhj/article/details/38796739)。有朋友私信问我关于ACTION_SCREEN_ON和ACTION_SCREEN_OFF还有ACTION_USER_PRESENT三个Action的使用方法,因为作为一个总结博文,当时并没有具体讲,ACTION_SCREEN_ON和ACTION_SCREEN_OFF仅仅能通过动态注冊的方式(代码内context.register和unregister)。而ACTION_USER_PRESENT则是动态、静态注冊两种方式都能够。

以下我们通过这个锁屏、解锁相关的BroadcastReceiver来了解一下。


package cn.panghu.activitys;

import com.example.broadcastsappdemo.R;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ScreenLockedActivity extends Activity{
	private ScreenBroadcastReceiver screenBroadcastReceiver = null;
	private Context context = null;
	private Button lockedScreenBtn = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getApplicationContext();
		setContentView(R.layout.screen_lock_layout);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		//注冊这个广播
		registerScreenBroadcastReceiver();
	}
	
	private void registerScreenBroadcastReceiver() {
		screenBroadcastReceiver = new ScreenBroadcastReceiver();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_SCREEN_OFF);//当屏幕锁屏的时候触发
		intentFilter.addAction(Intent.ACTION_SCREEN_ON);//当屏幕解锁的时候触发
		intentFilter.addAction(Intent.ACTION_USER_PRESENT);//当用户又一次唤醒手持设备时触发
		context.registerReceiver(screenBroadcastReceiver, intentFilter);
		Log.i("screenBR", "screenBroadcastReceiver注冊了");
	}
	//重写广播
	class ScreenBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			String strAction = intent.getAction();
			if (Intent.ACTION_SCREEN_OFF.equals(strAction)){
				//屏幕锁屏
				Log.i("screenBR", "屏幕锁屏:ACTION_SCREEN_OFF触发");
				Toast.makeText(context, "锁屏了", Toast.LENGTH_SHORT).show();
			}else if (Intent.ACTION_SCREEN_ON.equals(strAction)){
				//屏幕解锁(实际測试效果,不能用这个来推断解锁屏幕事件)
				//【因为这个是解锁的时候触发。而解锁的时候广播还未注冊】
				Log.i("screenBR", "屏幕解锁:ACTION_SCREEN_ON触发");
				Toast.makeText(context, "解锁了", Toast.LENGTH_SHORT).show();
			}else if (Intent.ACTION_USER_PRESENT.equals(strAction)){
				//屏幕解锁(该Action能够通过静态注冊的方法注冊)
				//在解锁之后触发的,广播已注冊
				Log.i("screenBR", "屏幕解锁:ACTION_USER_PRESENT触发");
				Toast.makeText(context, "解锁了", Toast.LENGTH_SHORT).show();
			}else{
				//nothing
			}
		}
		
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		context.unregisterReceiver(screenBroadcastReceiver);
		Log.i("screenBR", "screenBroadcastReceiver取消注冊了");
	}
}


LogCat结果图:

技术分享


因为是静态注冊的方式,所以大家可能会认为那我要怎么让它长久地监听这锁屏、解锁屏幕的广播呢?

首先我们再次强调ACTION_SCREEN_ON和ACTION_SCREEN_OFF仅仅能通过动态注冊的方式(代码内context.register和unregister),而ACTION_USER_PRESENT则是动态、静态注冊两种方式都能够。


那么我们的突破口便是:我们能够动态地注冊一个关于屏幕解锁后(ACTION_USER_PRESENT)的广播者。而且在这个广播的onReceive方法中实现我们要做的一些操作。

比如我们能够开启一个Service服务。用于注冊我们所想要的这个Broadcast Receiver


1.在Service中定义receiver

[java] view plaincopy技术分享技术分享
  1. private BroadcastReceiver mScreenFilterReceiver = new BroadcastReceiver() {  
  2.     public void onReceive(Context context, Intent intent) {  
  3.             if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {  
  4.                 //做要求的处理  
  5.             }else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
  6. }  
  7.     }  
  8. };  

2.在Service的onCreate中定义IntentFilter及注冊receiver

[java] view plaincopy技术分享技术分享
  1. IntentFilter ScreenFilter = new IntentFilter();  
  2. ScreenFilter.addAction(Intent.ACTION_SCREEN_ON);  
  3. ScreenFilter.addAction(Intent.ACTION_SCREEN_OFF); 
  4. registerReceiver(mScreenFilterReceiver, ScreenFilter);  

3.在Service的onDestroy中要反注冊这个receiver。

[java] view plaincopy技术分享技术分享
  1. unregisterReceiver(mScreenFilterReceiver);  

BroadcastReceiver之实现锁屏、解锁样例