您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这期内容当中小编将会给大家带来有关怎么在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。