Android 监听锁屏、解锁、开屏 功能代码
程序员文章站
2024-03-03 14:39:22
1、首先定义 screenlistener
package com.app.lib;
import android.content.broadcastrece...
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(); } }); } }
以上所述是小编给大家介绍的android 监听锁屏、解锁、开屏 功能代码的相关知识,希望对大家有所帮助!
上一篇: Java实现插入排序实例
下一篇: Java连接各种数据库的方法