多平台统一管理软件接口,如何实现多平台统一管理软件接口
583
2023-01-16
Android Home键监听的实现代码
之前的一个项目需要监听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 {
privathttp://e 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
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;
}
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~