标签:des android style blog http color os java io
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() {
-
- 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)){
-
-
- Log.i("screenBR", "屏幕解锁:ACTION_USER_PRESENT触发");
- Toast.makeText(context, "解锁了", Toast.LENGTH_SHORT).show();
- }else{
-
- }
- }
-
- }
- @Override
- protected void onPause() {
-
- 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
- private BroadcastReceiver mScreenFilterReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
-
- }else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
- }
- }
- };
2.在Service的onCreate中定义IntentFilter及注册receiver
- IntentFilter ScreenFilter = new IntentFilter();
- ScreenFilter.addAction(Intent.ACTION_SCREEN_ON);
- ScreenFilter.addAction(Intent.ACTION_SCREEN_OFF);
- registerReceiver(mScreenFilterReceiver, ScreenFilter);
3.在Service的onDestroy中要反注册这个receiver。
- unregisterReceiver(mScreenFilterReceiver);
BroadcastReceiver之实现锁屏、解锁例子
标签:des android style blog http color os java io
原文地址:http://www.cnblogs.com/yido9932/p/3937026.html