首先是一个封装类
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(); } }
用法::::::::::::::::::::::::::::::::::::::::::::::::::::::::::在主的界面上
//订阅 screenObserver.requestScreenStateUpdate(this);实现接口
//亮屏 @Override public void onScreenOn() { Log.d("xxxxx", "亮屏"); } //息屏 @Override public void onScreenOff() { Log.d("xxxxx", "息屏"); } //锁屏 @Override public void onUserPresent() { Log.d("xxxxx", "锁屏"); }