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);
时间: 2024-08-28 21:16:09

BroadcastReceiver之实现锁屏、解锁例子的相关文章

锁屏解锁

package com.example.lock_screen; import android.app.Activity; import android.content.IntentFilter; import android.os.Bundle; public class MainActivity extends Activity { private LockScreenReceiver lockScreenReceiver; @Override protected void onCreate

Android 通话:后台通话过程中锁屏解锁,发现听筒内存在锁屏提示音

[系统版本]:T0316 [ HW ]:p1 [测试前提]:1.卡1插入移动4G卡 [测试步骤]:1.呼入或呼出一通来电,接听: 2.按home返回后台,锁屏解锁查看. [测试结果]:1.后台通话过程中锁屏解锁,发现听筒内存在锁屏提示音 [预期结果]:1.通话过程中不应存在锁屏提示音 [概 率]:必现 solution: 修改文件:./frameworks/av/services/audiopolicy/AudioPolicyManager.cpp status_t AudioPolicyMan

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仅仅能通过动态注冊的方式(代码内co

BroadcastReceiver之屏幕锁屏和解锁监听

对于解锁和锁屏这种用的比较频繁action,谷歌做了限制,必须手动用代码注册 直接上代码:这是注册广播 1 public class MainActivity extends AppCompatActivity { 2 Screen screen; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.l

Android 锁屏监听

package com.example.lockscreenlistenerdemo; public interface LockScreenListener { String Tag="LockScreenListener"; public void onScreenOn(); public void onScreenOff(); public void onUserPresent(); } 做一个监听接口. 监听广播,监听锁屏 解锁  点亮屏幕的Action package com

[每日app二]月入60万多嘛?单词锁屏的潜力!

抢了用户的时间,就是抢了用户的金钱! 单词锁屏,一个开发难度不太大,但仅仅360手机助手下载就是每周4万!拉风- 对于搞app的同学来说,搞个锁屏,还不是玩似的,但是要定位好,玩得好,那就有难度了.最重要的是这个满大街广告横飞的世界,人家做到无 广告,无支付,被用户好评9.1那也是正常能理解的事情了. 还有一件事情就是,千万要记住,现在不收费,不等于以后不收,或者说不等于以后没有盈利模式. 废话不想多说,直接安装“单词锁屏”! 看到没?人家在Splash界面写着“让没有意义的解锁变为有意义的事情

Appium 解决锁屏截屏问题(java篇)

今天有个小伙伴问我,怎么把锁屏进行解锁操作?   A.思路在初始化driver后,加入等待判断是否有锁屏(元素)(记得要加入等待) B.如果有就进行解锁,就一般的输入数字密码然后进行解锁(当然了你要知道如何解锁手势密码请参考我的另一篇博客) 下面先上个图,可以参考看看,整体请结合自己的手机做相应处理 多做事,少说话看图: 看代码:   //手机锁屏解锁 @Test public void lockCreen(){ try { Thread.sleep(5000);//记得要等待 } catch

屏幕锁屏以及解锁监听

屏幕锁屏以及解锁时会分别发送两个广播SCREEN_ON和SCREEN_OFF,但是这两个action只能通过代码的形式注册才能被监听到,在AndroidManifest.xml中注册根本监听不到. public class ScreenActionReceiver extends BroadcastReceiver { private String TAG = "ScreenActionReceiver"; private boolean isRegisterReceiver = fa

安卓开发之动态注册广播(锁屏和解锁案例)

package com.lidaochen.test001; import android.content.Intent; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivit