HardBirch

【Android代码片段之八】监听Android屏幕是否锁屏

时间:12-05-28 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:1 点击: 2,256 次

实现方法: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();
	}
}

源码:点击下载源码

【Android代码片段之八】监听Android屏幕是否锁屏:目前有1 条留言

  1. 沙发
    gezhibo1985:

    很好,很实用,还有什么其他NB功能吗?邮箱交流
    640550537@qq.com 谢谢。

    2012-07-18 21:01 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐