如何在Android 4.0中覆盖主页按钮 [英] How to override home button in android 4.0
本文介绍了如何在Android 4.0中覆盖主页按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的android活动中覆盖主页"按钮.我已经尝试了一些与此相关的东西,这些东西适用于2.3及以下版本,但不适用于4.0及以上版本
I want to override the home button in my android activity. I have tried out few things related to this which are working for 2.3 and below but not for 4.0 above
@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();
}
但是它没有帮助我.有任何想法请分享信息
But its not helping me. any have idea about it please share information
推荐答案
我也遇到了这样的问题,并且我正在使用以下类来监听主屏幕按钮单击事件.
I also encountered such a problem and I am using the following class to listen to the home button click event.
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 4.0中覆盖主页按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文