網(wǎng)上有很多監(jiān)聽home鍵的方法但是有很多都監(jiān)聽不到,今天總結(jié)一個(gè)相對(duì)來說比較方便和簡單的方法惩阶。
當(dāng)用戶點(diǎn)擊系統(tǒng)層級(jí)的按鍵的時(shí)候都會(huì)發(fā)出系統(tǒng)廣播挎狸,這個(gè)時(shí)候我們只要register相應(yīng)的廣播就可以獲取當(dāng)相應(yīng)事件了,需要注意的是断楷,靜態(tài)注冊(cè)不可以~~只能在代碼里面動(dòng)態(tài)注冊(cè)锨匆!我是在application里面直接注冊(cè),這樣在整個(gè)app中都能接收到冬筒。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class HomeKeyListener{
static final String TAG = "HomeKeyWatcher";
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private HomeKeyRecevier mRecevier;
// 回調(diào)接口
public interface OnHomePressedListener {
public void onHomePressed();
public void onHomeLongPressed();
}
public HomeKeyListener(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}
/**
* 設(shè)置監(jiān)聽
*
* @param listener
*/
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
mRecevier = new HomeKeyRecevier();
}
/**
* 開始監(jiān)聽恐锣,注冊(cè)廣播
*/
public void startWatch() {
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}
/**
* 停止監(jiān)聽,注銷廣播
*/
public void stopWatch() {
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}
/**
* 廣播接收者
*/
class HomeKeyRecevier 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.e(TAG, "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();
}
}
}
}
}
}
}
在application中注冊(cè)
public class OBDApplication extends Application{
private HomeKeyWatcher HomeKeyListener;
@Override
public void onCreate() {
super.onCreate();
}
private void initHomeKeyListener() {
HomeKeyListener = new HomeKeyListener(this);
HomeKeyListener.setOnHomePressedListener(new HomeKeyWatcher.OnHomePressedListener() {
@Override
public void onHomePressed() {
WindowUtils.hidePopupWindow();
}
@Override
public void onHomeLongPressed() {
}
});
HomeKeyListener.startWatch();
}
@Override
public void onTerminate() {
super.onTerminate();
HomeKeyListener.stopWatch();
}
}
像網(wǎng)上有很多說監(jiān)聽home鍵在onKeyDown中判斷KeyEvent.ACTION_DOWN (KEYCODE_HOME)直接就可以獲取相應(yīng)的監(jiān)聽舞痰,但是土榴,我試了一下,根本攔截不到响牛,查找了一下原因
/** Key code constant: Home key.
* This key is handled by the framework and is never delivered to applications. */
public static final int KEYCODE_HOME = 3;
原因
這個(gè)事件直接被framework層給截獲了玷禽,never delivered!娃善!永遠(yuǎn)不會(huì)傳遞的application層B垩堋!聚磺!坯台,所以在onKeyDown處理是拿不到事件的!L鼻蕖蜒蕾!