您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在Android中監聽Home鍵,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
注冊廣播:
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; } } } }
上述就是小編為大家分享的怎么在Android中監聽Home鍵了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。