怎么在Android中监听Home键
这期内容当中小编将会给大家带来有关怎么在Android中监听Home键,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联建站是专业的青岛网站建设公司,青岛接单;提供成都做网站、成都网站建设、成都外贸网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行青岛网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
Android是什么
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 ListmPressListeners = new ArrayList (); private HomeReceiver mReceiver; private IntentFilter mHomeFileter; private HomePressListener mHomePressListener; public void init() { if (isDestroy.get()) { this.mPressListeners = new ArrayList (); 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键了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
当前名称:怎么在Android中监听Home键
本文地址:http://azwzsj.com/article/jeedjj.html