【Android代码片段之八】监听Android屏幕是否锁屏
实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可以判断屏幕状态是否锁屏,但是只有屏幕状态发生改变时才会发出广播;
2)如果要在屏幕状态发生改变之前就想获取屏幕状态,可以通过反射机制调用PowerManager的isScreenOn方法 。
具体实现,见代码:
实现Screen状态监听的类ScreenObserver,实现如下:
- package com.isoft.screen;
- import java.lang.reflect.Method;
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.PowerManager;
- import android.util.Log;
- /**
- *
- * @author zhangyg
- *
- */
- public class ScreenObserver{
- private static String TAG = "ScreenObserver";
- private Context mContext;
- private ScreenBroadcastReceiver mScreenReceiver;
- private ScreenStateListener mScreenStateListener;
- private static Method mReflectScreenState;
- public ScreenObserver(Context context){
- mContext = context;
- mScreenReceiver = new ScreenBroadcastReceiver();
- try {
- mReflectScreenState = PowerManager.class.getMethod("isScreenOn",
- new Class[] {});
- } catch (NoSuchMethodException nsme) {
- Log.d(TAG, "API < 7," + nsme);
- }
- }
- /**
- * screen状态广播接收者
- * @author zhangyg
- *
- */
- private class ScreenBroadcastReceiver extends BroadcastReceiver{
- private String action = null;
- @Override
- public void onReceive(Context context, Intent intent) {
- action = intent.getAction();
- if(Intent.ACTION_SCREEN_ON.equals(action)){
- mScreenStateListener.onScreenOn();
- }else if(Intent.ACTION_SCREEN_OFF.equals(action)){
- mScreenStateListener.onScreenOff();
- }
- }
- }
- /**
- * 请求screen状态更新
- * @param listener
- */
- public void requestScreenStateUpdate(ScreenStateListener listener) {
- mScreenStateListener = listener;
- startScreenBroadcastReceiver();
- firstGetScreenState();
- }
- /**
- * 第一次请求screen状态
- */
- private void firstGetScreenState(){
- PowerManager manager = (PowerManager) mContext
- .getSystemService(Activity.POWER_SERVICE);
- if (isScreenOn(manager)) {
- if (mScreenStateListener != null) {
- mScreenStateListener.onScreenOn();
- }
- } else {
- if (mScreenStateListener != null) {
- mScreenStateListener.onScreenOff();
- }
- }
- }
- /**
- * 停止screen状态更新
- */
- public void stopScreenStateUpdate(){
- mContext.unregisterReceiver(mScreenReceiver);
- }
- /**
- * 启动screen状态广播接收器
- */
- private void startScreenBroadcastReceiver(){
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_SCREEN_ON);
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- mContext.registerReceiver(mScreenReceiver, filter);
- }
- /**
- * screen是否打开状态
- * @param pm
- * @return
- */
- private static boolean isScreenOn(PowerManager pm) {
- boolean screenState;
- try {
- screenState = (Boolean) mReflectScreenState.invoke(pm);
- } catch (Exception e) {
- screenState = false;
- }
- return screenState;
- }
- public interface ScreenStateListener {
- public void onScreenOn();
- public void onScreenOff();
- }
- }
测试ScreenObserver功能的示例类ScreenObserverActivity,实现如下:
- package com.isoft.screen;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import com.isoft.screen.ScreenObserver.ScreenStateListener;
- public class ScreenObserverActivity extends Activity {
- private String TAG = "ScreenObserverActivity";
- private ScreenObserver mScreenObserver;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mScreenObserver = new ScreenObserver(this);
- mScreenObserver.requestScreenStateUpdate(new ScreenStateListener() {
- @Override
- public void onScreenOn() {
- doSomethingOnScreenOn();
- }
- @Override
- public void onScreenOff() {
- doSomethingOnScreenOff();
- }
- });
- }
- private void doSomethingOnScreenOn() {
- Log.i(TAG, "Screen is on");
- }
- private void doSomethingOnScreenOff() {
- Log.i(TAG, "Screen is off");
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- //停止监听screen状态
- mScreenObserver.stopScreenStateUpdate();
- }
- }
- http://blog.csdn.net/m_changgong/article/details/7608911