Android 监听手机锁屏

Android 监听手机锁屏,第1张

概述自定义 ScreenListener package com.example.teagardenhd.Listener;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.Inten 自定义 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() {        //解锁了    }});
原博客地址 链接地址 总结

以上是内存溢出为你收集整理的Android 监听手机锁屏全部内容,希望文章能够帮你解决Android 监听手机锁屏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存