gpt4 book ai didi

android - 如何覆盖 android 4.0 中的主页按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:37 27 4
gpt4 key购买 nike

我想覆盖我的 android Activity 中的主页按钮。我已经尝试了一些与此相关的东西,它们适用于 2.3 及以下但不适用于 4.0 以上

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {

if(keyCode == KeyEvent.KEYCODE_HOME)
{
startActivity(new Intent(this, ActivityB.class));
return true;
}
return super.onKeyDown(keyCode, event); }

还有其他方法

 @Override public void onAttachedToWindow() { 
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

但这对我没有帮助。有任何想法请分享信息

最佳答案

我也遇到了这样的问题,我正在使用下面的类来监听home键的点击事件。

public class HomeWatcher {

static final String TAG = "HomeWatcher";

private Context mContext;

private IntentFilter mFilter;

private OnHomePressedListener mListener;

private InnerRecevier mRecevier;

public HomeWatcher(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}

/**
* set the home pressed listener, if set will callback the home pressed
* listener's method when home pressed.
*
* @param listener
*/
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
mRecevier = new InnerRecevier();
}

/**
* start watch
*/
public void startWatch() {
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}

/**
* stop watch
*/
public void stopWatch() {
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}

class InnerRecevier extends BroadcastReceiver {

final String SYSTEM_DIALOG_REASON_KEY = "reason";

final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";

final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null) {
Log.i(TAG, "receive action:" + action + ",reason:" + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {

// home?
mListener.onHomePressed();
} else if (reason
.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {

// ??home?
mListener.onHomeLongPressed();
}
}
}
}
}

}

}

用法如下:

HomeWatcher mHomeWatcher = new HomeWatcher(Context);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {

public void onHomePressed() {

//do your somthing...
}

public void onHomeLongPressed() {
}

});

mHomeWatcher.startWatch();

关于android - 如何覆盖 android 4.0 中的主页按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22952794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com