Android系统亮屏、锁屏、屏幕解锁事件(解决部分手机亮屏后未解锁即进入resume状态)


有一部分三星的手机关闭屏幕以后,刚打开屏幕,前台的activity就会resume状态,因为我们使用cocos2d-x引擎,默认resume和activity的resume事件是一致的,我们在resume里把关闭的声音重新打开,所以导致,用户点亮屏幕以后(尚未解锁),就会听到游戏内的声音(如果在关闭屏幕时我们游戏处于前台)。


为了解决这个问题,我们需要修改我们resume触发的时机。确保在解锁以后才进入我们游戏的resume状态。
那么我们游戏应该在什么时机进入resume状态呢?
  1. 系统activity进入resume状态时刻,并且屏幕是亮的,屏幕未锁定
  2. 屏幕刚刚亮起,并且屏幕 未锁定,系统activity已经进入resume状态
  3. 屏幕 解锁,这是屏幕肯定是亮的,并且activity已经进入resume状态

那么有了上面的三点相信逻辑代码不难写,下面介绍一下如何坚挺系统屏幕灭,屏幕亮,解锁屏幕 的事件,以及如何判断当前屏幕 是否是解锁状态(因为用户可以设置关闭屏幕后是否锁定屏幕 ,所以解锁屏幕 事件可能根本就不会被触发,那么我们就不能通过跟踪事件来确定其状态)

要坚挺屏幕亮、屏幕灭、屏幕 解锁三个事件我们需要在代码中注册监听ACTION_SCREEN_ONACTION_SCREEN_OFFACTION_USER_PRESENT三个Action,具体的写法如下:

	/**
	 * screen状态广播接收者
	 */
	private class ScreenBroadcastReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			if(Intent.ACTION_SCREEN_ON.equals(intent.getAction())){				
				mScreenStateListener.onScreenOn();
			}else if(Intent.ACTION_SCREEN_OFF.equals(intent.getAction())){
				mScreenStateListener.onScreenOff();
			}else if(Intent.ACTION_USER_PRESENT.equals(intent.getAction())){
				mScreenStateListener.onUserPresent();
			}
		}
	}

	/**
	 * 停止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);
		filter.addAction(Intent.ACTION_USER_PRESENT);
		mContext.registerReceiver(mScreenReceiver, filter);
	}


因为屏幕在灭了以后是否锁定可以由用户设置,所以我们不能通过跟踪解锁事件来判断其状态,需要一个可以时时获取状态的函数:
	//判断屏幕是否被锁定
	public final static boolean isScreenLocked(Context c) {
		android.app.KeyguardManager mKeyguardManager = (KeyguardManager) c.getSystemService(c.KEYGUARD_SERVICE);
		return mKeyguardManager.inKeyguardRestrictedInputMode();
	}

需要注意的是,大部分事件都可以在AndroidManifest.xml中注册,但是ACTION_SCREEN_ON、ACTION_SCREEN_OFF、ACTION_USER_PRESENT这几个事件在PowerManager那边做了限制,必须像上面那样在代码中注册,才能监听到,所以大家不要踩到坑哦~~~

下面是包装屏幕事件的一个类代码,仅供参考:
package com.example.myfirstapp;

import java.lang.reflect.Method;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
import android.util.Log;

/**
 * 监听屏幕ON和OFF PRESENT状态
 * 
 * @author
 * @2014
 * 
 */
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 (Exception nsme) {
			Log.d(TAG, "API < 7," + nsme);
		}
	}

	/**
	 * screen状态广播接收者
	 */
	private class ScreenBroadcastReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
				mScreenStateListener.onScreenOn();
			} else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
				mScreenStateListener.onScreenOff();
			} else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
				mScreenStateListener.onUserPresent();
			}
		}
	}

	/**
	 * 请求screen状态更新
	 */
	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);
		filter.addAction(Intent.ACTION_USER_PRESENT);
		mContext.registerReceiver(mScreenReceiver, filter);
	}

	/**
	 * screen是否打开状态
	 */
	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();

		public void onUserPresent();
	}

	public final static boolean isScreenLocked(Context c) {
		android.app.KeyguardManager mKeyguardManager = (KeyguardManager) c
				.getSystemService(c.KEYGUARD_SERVICE);
		return mKeyguardManager.inKeyguardRestrictedInputMode();
	}
}


上面代码参考:http://blog.csdn.net/m_changgong/article/details/7608911,特此注明。


Android系统亮屏、锁屏、屏幕解锁事件(解决部分手机亮屏后未解锁即进入resume状态),布布扣,bubuko.com

Android系统亮屏、锁屏、屏幕解锁事件(解决部分手机亮屏后未解锁即进入resume状态)

上一篇:App Store idfa被拒检查办法


下一篇:通过APPID从appStore获取应用最新信息