Android监听屏幕屏幕锁屏与解锁

/**
 * 创建一个监听器类 监听android锁屏与解锁事件
 */
public class ScreenListener {
    private Context mContext;
    private ScreenBroadcastReceiver mScreenReceiver;
    private ScreenStateListener mScreenStateListener;

    public ScreenListener(Context context) {
        mContext = context;
        mScreenReceiver = new ScreenBroadcastReceiver();
    }

    /**
     * screen状态广播接收者
     */
    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();
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
                mScreenStateListener.onUserPresent();
            }
        }
    }

    /**
     * 开始监听screen状态
     *
     * @param listener
     */
    public void begin(ScreenStateListener listener) {
        mScreenStateListener = listener;
        registerListener();
        getScreenState();
    }

    /**
     * 获取screen状态
     */
    private void getScreenState() {
        PowerManager manager = (PowerManager) mContext
                .getSystemService(Context.POWER_SERVICE);
        if (manager.isScreenOn()) {
            if (mScreenStateListener != null) {
                mScreenStateListener.onScreenOn();
            }
        } else {
            if (mScreenStateListener != null) {
                mScreenStateListener.onScreenOff();
            }
        }
    }

    /**
     * 停止screen状态监听
     */
    public void unregisterListener() {
        mContext.unregisterReceiver(mScreenReceiver);
    }

    /**
     * 启动screen状态广播接收器
     */
    private void registerListener() {
        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 interface ScreenStateListener {// 返回给调用者屏幕状态信息
        public void onScreenOn();

        public void onScreenOff();

        public void onUserPresent();
    }
}
 
  
在需要监听解锁或锁屏的Activity或者Fragment里边添加
 
  
ScreenListener mScreenListener;
在初始化onCreate或者 onCreateView里边初始化监听器
mScreenListener=new ScreenListener(this);
mScreenListener.begin(new ScreenListener.ScreenStateListener() {
    @Override
    public void onUserPresent() {
        DebugLog.i("onUserPresent");
    }
    @Override
    public void onScreenOn() {
        DebugLog.i("onScreenOn");
    }

    @Override
    public void onScreenOff() {
        DebugLog.i("onScreenOff");
      	...
    }
});
注意监听的Activity或者Fragment销毁时,要取消监听广播事件
@Override
public void onDestroy(){
    super.onDestroy();
    DebugLog.i("onDestroy");
    mScreenListener.unregisterListener();
}
 
  
最后注意在AndroidManifest.xml里边注册权限
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />

你可能感兴趣的:(Android开发)