天天看点

Android定义亮屏、灭屏、锁屏、退出应用监听器

有的时候我们的项目需要监听手机亮屏灭屏锁屏等状态,来做一些自己的事,比如解锁后要求用户输入app密码等。 由于屏幕状态变化时,系统会发出对应的广播,这时候我们可以定义一个监听器来接收广播,然后做一些对应的事。 我们需要首先创建一个类ScreenListener,然后在这个class里面定义一个接口ScreenStateListener,接口里面写亮屏、灭屏、解锁、退出应用时的操作:

public class ScreenListener {

    ......

    public interface ScreenStateListener {
        public void onScreenOn();

        public void onScreenoff();

        public void onUserPrisent();

        public void exitAPP();
    }

           

为了调用这个接口里面的方法,需要有这个接口的实现类的对象ScreenStateListener stateListener,当接收到广播后通过这个stateListener来调用onScreenOn()、onScreenoff()、onUserPrisent()、exitAPP()方法。由于我们需要在这些方法中使用和处理activity中的变量等,

所以stateListener一定是在activity中创建的,然后需要通过listener的构造方法或者其他方法把这个实现类对象传递过来。

为了接收到系统的广播,我们需要在ScreenListener.class里面写一个内部类classScreenBroadcastReceiverextendsBroadcastReceiver来接收广播,并重写其中的onReceive方法:

/*
    * Screen状态广播接收者
    * */
    private class ScreenBroadcastReceiver extends BroadcastReceiver {
        private String action;

        /*
        * 当接收到广播时将会运行这个方法
        * */
        @Override
        public void onReceive(Context context, Intent intent) {

            action = intent.getAction();
            switch (action) {
                case Intent.ACTION_SCREEN_ON:   //开屏
                    stateListener.onScreenOn();
                    break;
                case Intent.ACTION_SCREEN_OFF:  //锁屏
                    stateListener.onScreenoff();
                    break;
                case Intent.ACTION_USER_PRESENT:    //解锁
                    stateListener.onUserPrisent();
                    break;
                case "ExitAPP":         //退出APP
                    stateListener.exitAPP();
                    break;
            }
        }
    }
           

既然用到了广播,那么就涉及到了注册和反注册广播,所以我们需要写这样两个方法:registListener()、unregistListener()。当需要监听广播时,我们就就注册监听器,当不需要监听时我们就反注册。这两个方法中分别对广播进行注册registerReceiver和反注册unregisterReceiver,由于注册和反注册广播都是Context的方法,所以我们还要在创建screenBroadcastReceiver的时候传入activity的Context。

传递stateListener对象的方法我是写了一个begin(ScreenStateListenerstateListener)方法,调用此方法开始监听(注册广播)。

好了,基本的思路已经说完了,那么下面我将会给出这个Listener的全部代码:

/**
 * Created by 泥巴城  on 2016/11/21.
 * 监听屏幕亮,灭,解锁
 */

public class ScreenListener {
    private Context context;
    private ScreenBroadcastReceiver screenBroadcastReceiver;
    private ScreenStateListener stateListener;

    public ScreenListener(Context context) {
        this.context = context;
        screenBroadcastReceiver = new ScreenBroadcastReceiver();
    }

    /*
    * Screen状态广播接收者
    * */
    private class ScreenBroadcastReceiver extends BroadcastReceiver {
        private String action;

        /*
        * 当接收到广播时将会运行这个方法
        * */
        @Override
        public void onReceive(Context context, Intent intent) {

            action = intent.getAction();
            switch (action) {
                case Intent.ACTION_SCREEN_ON:   //开屏
                    stateListener.onScreenOn();
                    break;
                case Intent.ACTION_SCREEN_OFF:  //锁屏
                    stateListener.onScreenoff();
                    break;
                case Intent.ACTION_USER_PRESENT:    //解锁
                    stateListener.onUserPrisent();
                    break;
                case "ExitAPP":         //退出APP
                    stateListener.exitAPP();
                    break;
            }
        }
    }

    /*
    * 启动screen状态广播接收器
    * */
    private void registListener() {
        IntentFilter filter = new IntentFilter();   //filter将会设置监听的地点,当有操作就会发出广播
        filter.addAction("ExitAPP");
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        context.registerReceiver(screenBroadcastReceiver, filter);
    }

    /*
    * 当不需要监听屏幕时,使用此方法可以解除绑定
    * */
    public void unregistListener() {
        context.unregisterReceiver(screenBroadcastReceiver);
    }

    /*
    * 获取屏幕状态并直接运行该状态下的操作
    * */
    private void getScreenState() {
        PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (manager.isScreenOn()) {
            if (stateListener != null) {
                stateListener.onScreenOn();
            }
        } else {
            if (stateListener != null) {
                stateListener.onScreenoff();
            }
        }

    }

    /*
    * 直接使用类2的对象调用此函数,开始监听屏幕
    * */
    public void begin(ScreenStateListener stateListener) {
        this.stateListener = stateListener;
        registListener();
        getScreenState();
    }

    /*
    * 提供给用户,用来设置屏幕解锁,锁屏的操作
    * */
    public interface ScreenStateListener {
        public void onScreenOn();

        public void onScreenoff();

        public void onUserPrisent();

        public void exitAPP();
    }
}
           

监听器写好了,那么activity怎么用呢?下面给出代码:

在activity的OnCreate方法中写一下代码:

//监听锁屏状态 和 退出程序信号
        ScreenListener listener = new ScreenListener(context);
        listener.begin(new ScreenListener.ScreenStateListener() {
            @Override
            public void onScreenOn() {

            }

            @Override
            public void onScreenoff() {

            }

            @Override
            public void onUserPrisent() {
                if (useWhat != 0) {

                    Intent intent = new Intent();
                    switch (useWhat) {

                        case 1:
                            intent.setClass(context, DLockActivity.class);
                            break;
                        case 2:
                            intent.setClass(context, PLockActivity.class);
                            break;
                        case 3:
                            intent.setClass(context, DIYLockActivity.class);
                            break;
                    }
                    intent.putExtra("flag", "openScreen");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                }
            }

            @Override
            public void exitAPP() {
                finish();
            }
        });

    }
           

这样,activity就开始监听屏幕了,当屏幕有相应的广播发出来的时候,我们就能监听到。而且,我相信大家已经注意到了ExitAPP这个广播,这是自定义的一个广播,当我们需要退出应用的时候,我们只需要调用以下代码,就可以让所有监听屏幕的activity销毁,以达到退出应用的目的:

getApplicationContext().sendBroadcast(newIntent("ExitAPP"));

最后提醒大家,为了节约宝贵的资源,不要忘记在onDestory方法中反注册监听器哦!!