Android Home键监听的实现代码
之前的一个项目需要监听home键的事件,发现用dispatchKeyEvent无法实现监听,后来查了些资料,才知道home键不能用常规的按键事件监听,需要使用广播的方法。下面是我封装的一个工具类。根据自己实际需要修改吧。
注册广播:
HomeListener.getInstance().start(this);
移除广播
HomeListener.getInstance().stop(this);
设置监听回调
HomeListener.getInstance().setHomeKeylistener(HomePressListenerlistener);
添加监听回调
HomeListener.getInstance().addHomeKeyListener(HomePressListenerlistener)
移除监听回调
HomeListener.getInstance().removeHomeKeyListener(HomePressListenerlistener);
移除所有监听回调
HomeListener.getInstance().removeAllHomeKeyListener();
销毁
HomeListener.getInstance().destroy();
工具类代码:
importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importjava.util.ArrayList; importjava.util.List; importjava.util.concurrent.atomic.AtomicBoolean; publicclassHomeListener{ publicstaticHomeKeyListenergetInstance(){ HomeKeyListenerlistener=HomeKeyListener.sListener; listener.init(); returnlistener; } staticclassHomeKeyListener{ privatestaticfinalStringSYSTEM_DIALOG_REASON_RECENT_APPS="recentapps";//home键旁边的最近程序列表键 privatestaticfinalStringSYSTEM_DIALOG_REASON_HOME_KEY="homekey";//按下home键 privatestaticfinalStringSYSTEM_DIALOG_REASON_ASSIST="assist";//某些三星手机的程序列表键 privatestaticAtomicBooleanisDestroy=newAtomicBoolean(true); privatestaticAtomicBooleanisRegister=newAtomicBoolean(false); privatestaticHomeKeyListenersListener=newHomeKeyListener(); privateListmPressListeners=newArrayList (); privateHomeReceivermReceiver; privateIntentFiltermHomeFileter; privateHomePressListenermHomePressListener; publicvoidinit(){ if(isDestroy.get()){ this.mPressListeners=newArrayList (); this.mReceiver=newHomeReceiver(); this.mHomeFileter=newIntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); isDestroy.set(false); } } publicsynchronizedvoidstart(Contextcontext){ if(!isRegister.get()){ context.registerReceiver(mReceiver,mHomeFileter); isRegister.set(true); } } publicsynchronizedvoidstop(Contextcontext){ if(isRegister.get()){ context.unregisterReceiver(mReceiver); isRegister.set(false); } } publicvoidsetHomeKeylistener(HomePressListenerlistener){ this.mHomePressListener=listener; } publicvoidaddHomeKeyListener(HomePressListenerlistener){ mPressListeners.add(listener); } publicvoidremoveHomeKeyListener(HomePressListenerlistener){ mPressListeners.add(listener); } publicvoidremoveAllHomeKeyListener(){ mPressListeners.clear(); } publicvoiddestroy(){ this.mPressListeners.clear(); this.mPressListeners=null; this.mReceiver=null; this.mHomeFileter=null; this.mHomePressListener=null; isDestroy.set(true); } publicinterfaceHomePressListener{ voidonHomePress(); voidonHomeRecentAppsPress(); } classHomeReceiverextendsBroadcastReceiver{ @Override publicvoidonReceive(Contextcontext,Intentintent){ receive(intent); } } privatevoidreceive(Intentintent){ Stringaction=intent.getAction(); if(action!=null&&action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){ Stringreason=intent.getStringExtra("reason"); dispatchPress(reason); } } privatevoiddispatchPress(Stringreason){ switch(reason){ caseSYSTEM_DIALOG_REASON_HOME_KEY: if(mHomePressListener!=null)mHomePressListener.onHomePress(); for(HomePressListenerlistener:mPressListeners)listener.onHomePress(); break; caseSYSTEM_DIALOG_REASON_RECENT_APPS: caseSYSTEM_DIALOG_REASON_ASSIST: if(mHomePressListener!=null)mHomePressListener.onHomeRecentAppsPress(); for(HomePressListenerlistener:mPressListeners)listener.onHomeRecentAppsPress(); break; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。