main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textSize="20sp" android:text="监视是否按下电源键" /> </RelativeLayout>
MainActivity如下:
package com.cc.screenobservertest; import com.cc.util.ScreenObserver; import com.cc.util.ScreenObserver.observerScreenStateUpdateListener; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 监听按下电源键锁屏以及再次按下电源键解锁 */ public class MainActivity extends Activity { private ScreenObserver mScreenObserver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initScreenObserver(); } private void initScreenObserver(){ mScreenObserver = new ScreenObserver(MainActivity.this); mScreenObserver.observerScreenStateUpdate(new observerScreenStateUpdateListener() { @Override public void onScreenOn() { whenScreenOn(); } @Override public void onScreenOff() { whenScreenOnOff(); } }); } private void whenScreenOn() { System.out.println(" 按下电源键,屏幕现在打开 "); } private void whenScreenOnOff() { System.out.println(" 按下电源键,屏幕现在关闭 "); } @Override protected void onDestroy() { super.onDestroy(); if (mScreenObserver!=null) { mScreenObserver.stopScreenStateUpdate(); } } }
ScreenObserver.java如下:
package com.cc.util; 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; public class ScreenObserver { private Context mContext; private ScreenBroadcastReceiver mScreenBroadcastReceiver; private observerScreenStateUpdateListener mObserverScreenStateUpdateListener; private static Method mReflectIsScreenOnMethod; public ScreenObserver(Context context) { mContext = context; mScreenBroadcastReceiver = new ScreenBroadcastReceiver(); try { mReflectIsScreenOnMethod = PowerManager.class.getMethod("isScreenOn",new Class[] {}); } catch (NoSuchMethodException nsme) { System.out.println("API<7不可使用使用"); } } //接收设备发出的广播 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)) { mObserverScreenStateUpdateListener.onScreenOn(); } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { mObserverScreenStateUpdateListener.onScreenOff(); } } } //此为入口. //监视屏幕状态 public void observerScreenStateUpdate(observerScreenStateUpdateListener listener) { mObserverScreenStateUpdateListener = listener; registerScreenBroadcastReceiver(); //该方法可以根据实际情况取舍 firstGetScreenState(); } // 注册监听 private void registerScreenBroadcastReceiver() { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); mContext.registerReceiver(mScreenBroadcastReceiver, filter); } //刚运行应用程序时判断屏幕状态 private void firstGetScreenState() { PowerManager manager = (PowerManager) mContext.getSystemService(Activity.POWER_SERVICE); if (isScreenOnForFirst(manager)) { if (mObserverScreenStateUpdateListener != null) { mObserverScreenStateUpdateListener.onScreenOn(); System.out.println("===> 刚运行程序时,屏幕为打开状态"); } } else { if (mObserverScreenStateUpdateListener != null) { mObserverScreenStateUpdateListener.onScreenOff(); System.out.println("===> 刚运行程序时,屏幕为关闭状态"); } } } //刚运行应用程序时判断屏幕是否关闭 private static boolean isScreenOnForFirst(PowerManager powerManager) { boolean screenState; try { screenState = (Boolean) mReflectIsScreenOnMethod.invoke(powerManager); //亦可采用下一句代码判断,从而避免使用反射 //screenState =powerManager.isScreenOn(); } catch (Exception e) { screenState = false; } return screenState; } //停止监视屏幕状态 public void stopScreenStateUpdate() { mContext.unregisterReceiver(mScreenBroadcastReceiver); } public interface observerScreenStateUpdateListener { public void onScreenOn(); public void onScreenOff(); } }