Android基础篇 监听屏幕锁屏

Posted 彭老希

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android基础篇 监听屏幕锁屏相关的知识,希望对你有一定的参考价值。

第一步:自定义ScreenListener

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;

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();
        }
    }

第二步:调用

package com.app.lib;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {

        private ScreenListener screenListener ;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            screenListener = new ScreenListener( MainActivity.this ) ;
            screenListener.begin(new ScreenListener.ScreenStateListener() {
                @Override
                public void onScreenOn() {
                    Toast.makeText( MainActivity.this , "屏幕打开了" , Toast.LENGTH_SHORT ).show();
                }

                @Override
                public void onScreenOff() {
                    Toast.makeText( MainActivity.this , "屏幕关闭了" , Toast.LENGTH_SHORT ).show();
                }

                @Override
                public void onUserPresent() {
                    Toast.makeText( MainActivity.this , "解锁了" , Toast.LENGTH_SHORT ).show();
                }
            });

        }
    }

以上是关于Android基础篇 监听屏幕锁屏的主要内容,如果未能解决你的问题,请参考以下文章

Android下屏幕锁屏弹窗的正确姿势

Android如何实现Android程序在手机锁屏后继续运行

Android 监听屏幕锁屏&用户解锁

让锁屏的指纹传感器在后台监听指纹,Android

BroadcastReceiver之屏幕锁屏和解锁监听

Android监听按键锁屏广播