• Android 监听手机锁屏的工具类


    自定义 ScreenListener

    package com.example.teagardenhd.Listener;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.PowerManager;
    /**
     * Created by ${zyj} on 2016/6/21.
     */
    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();
        }
    }

    实现监听器

    ScreenListener screenListener = new ScreenListener( MainActivity.this ) ;
    screenListener.begin(new ScreenListener.ScreenStateListener() {
        @Override
        public void onScreenOn() {
            //屏幕打开了
        }
        @Override
        public void onScreenOff() {
            //屏幕关闭了
    
        }
        @Override
        public void onUserPresent() {
            //解锁了
        }
    });

    原博客地址

    链接地址

  • 相关阅读:
    CSS3 3D转换
    CSS3 2D转换
    CSS3 字体
    CSS3 文本效果
    Spring核心技术(十三)——环境的抽象
    表达式求值
    一些设计上的原则
    POJ2503字典树
    MBR结构解析与fdisk的bash实现
    微服务指南走北(三):Restful API 设计简述
  • 原文地址:https://www.cnblogs.com/94xiyang/p/11121227.html
Copyright © 2020-2023  润新知