首页| 新闻| 娱乐| 游戏| 科普| 文学| 编程| 系统| 数据库| 建站| 学院| 产品| 网管| 维修| 办公| 热点
之前的一个项目需要监听home键的事件,发现用dispatchKeyEvent无法实现监听,后来查了些资料,才知道home键不能用常规的按键事件监听,需要使用广播的方法。下面是我封装的一个工具类。根据自己实际需要修改吧。
注册广播:
HomeListener.getInstance().start(this);
移除广播
HomeListener.getInstance().stop(this);
设置监听回调
HomeListener.getInstance().setHomeKeylistener(HomePressListener listener);
添加监听回调
HomeListener.getInstance().addHomeKeyListener(HomePressListener listener)
移除监听回调
HomeListener.getInstance().removeHomeKeyListener(HomePressListener listener);
移除所有监听回调
HomeListener.getInstance().removeAllHomeKeyListener();
销毁
HomeListener.getInstance().destroy();
工具类代码:
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import java.util.ArrayList;import java.util.List;import java.util.concurrent.atomic.AtomicBoolean;public class HomeListener { public static HomeKeyListener getInstance() { HomeKeyListener listener = HomeKeyListener.sListener; listener.init(); return listener; } static class HomeKeyListener { private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";//home键旁边的最近程序列表键 private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";//按下home键 private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";//某些三星手机的程序列表键 private static AtomicBoolean isDestroy = new AtomicBoolean(true); private static AtomicBoolean isRegister = new AtomicBoolean(false); private static HomeKeyListener sListener = new HomeKeyListener(); private List<HomePressListener> mPressListeners = new ArrayList<HomePressListener>(); private HomeReceiver mReceiver; private IntentFilter mHomeFileter; private HomePressListener mHomePressListener; public void init() { if (isDestroy.get()) { this.mPressListeners = new ArrayList<HomePressListener>(); this.mReceiver = new HomeReceiver(); this.mHomeFileter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); isDestroy.set(false); } } public synchronized void start(Context context) { if (!isRegister.get()) { context.registerReceiver(mReceiver, mHomeFileter); isRegister.set(true); } } public synchronized void stop(Context context) { if (isRegister.get()) { context.unregisterReceiver(mReceiver); isRegister.set(false); } } public void setHomeKeylistener(HomePressListener listener) { this.mHomePressListener = listener; } public void addHomeKeyListener(HomePressListener listener) { mPressListeners.add(listener); } public void removeHomeKeyListener(HomePressListener listener) { mPressListeners.add(listener); } public void removeAllHomeKeyListener() { mPressListeners.clear(); } public void destroy() { this.mPressListeners.clear(); this.mPressListeners = null; this.mReceiver = null; this.mHomeFileter = null; this.mHomePressListener = null; isDestroy.set(true); } public interface HomePressListener { void onHomePress(); void onHomeRecentAppsPress(); } class HomeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { receive(intent); } } private void receive(Intent intent) { String action = intent.getAction(); if (action != null && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = intent.getStringExtra( "reason" ); dispatchPress(reason); } } private void dispatchPress(String reason) { switch (reason) { case SYSTEM_DIALOG_REASON_HOME_KEY: if (mHomePressListener != null) mHomePressListener.onHomePress(); for (HomePressListener listener : mPressListeners) listener.onHomePress(); break; case SYSTEM_DIALOG_REASON_RECENT_APPS: case SYSTEM_DIALOG_REASON_ASSIST: if (mHomePressListener != null) mHomePressListener.onHomeRecentAppsPress(); for (HomePressListener listener : mPressListeners) listener.onHomeRecentAppsPress(); break; } } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VeVb武林网。
练就火眼金睛 十一种常见电脑
打印机共享提示“操作无法完成
如何查找有故障的配件
回眸一笑百魅生,六宫粉黛无颜色
岁月静美,剪一影烟雨江南
芜湖有个“松鼠小镇”
小满:小得盈满,一切刚刚好!
一串串晶莹剔透的葡萄,像一颗颗宝石挂在藤
正宗老北京脆皮烤鸭
人逢知己千杯少,喝酒搞笑图集
搞笑试卷,学生恶搞答题
新闻热点
疑难解答
图片精选
Kotlin结合Rxjava+Retrofit实现极
Kotlin与Java的主客观对比分析
Kotlin特性介绍及与Java 和 C#的简
Kotlin与Java哪个好?Kotlin与Java的
网友关注