之前的一个项目需要监听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;
}
}
}
}