博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 监听锁屏、解锁、开屏 操作
阅读量:6312 次
发布时间:2019-06-22

本文共 3518 字,大约阅读时间需要 11 分钟。

1、首先定义 ScreenListener 
package com.app.lib;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();    }}

 2、使用

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

  

 

 

转载地址:http://dgexa.baihongyu.com/

你可能感兴趣的文章
选择OA系统的价格误区
查看>>
网店日常经营 那些必须的步骤
查看>>
华为交换机基于SVI的VRRP多备份组
查看>>
Java使用HttpURLConnection上传文件
查看>>
XML学习总结(2)——XML简单介绍
查看>>
Ceph分布式集群文件系统测试
查看>>
RabbitMQ学习总结(7)——Spring整合RabbitMQ实例
查看>>
域名防封检测接口
查看>>
Linux系统加固之用户口令时效机制及GRUB安全设置
查看>>
KKK
查看>>
第七天 AFN、webView、https请求
查看>>
如何rename datafile name中存在乱码的数据文件
查看>>
Windows线程池
查看>>
java 堆排序 实现
查看>>
采用shell发布SVN项目到远程服务器
查看>>
redis工具类
查看>>
Redis持久化方式RDB与AOF详解
查看>>
TypeScript基础入门 - 接口 - 可索引的类型
查看>>
java以及jstl表达式中对HashMap的迭代
查看>>
node.js学习笔记之React
查看>>