Android捕獲home按鍵 以及實體鍵捕獲
阿新 • • 發佈:2018-12-18
1.有的android平臺並不支援home鍵長按,主要還是看framework裡面是否做了處理。
mHomeWatcher = new HomeWatcher(this); mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() { @Override public void onHomePressed() { Log.e(TAG, "onHomePressed"); } @Override public void onHomeLongPressed() { Log.e(TAG, "onHomeLongPressed"); } }); mHomeWatcher.startWatch(); }
home鍵捕獲類
public class HomeWatcher {
static final String TAG = "HomeWatcher"; private Context mContext; private IntentFilter mFilter; private OnHomePressedListener mListener; private BroadcastReceiver mRecevier; // 回撥介面 public interface OnHomePressedListener { public void onHomePressed(); public void onHomeLongPressed(); } public HomeWatcher(Context context) { mContext = context; mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); } /** * 設定監聽 * * @param listener */ public void setOnHomePressedListener(OnHomePressedListener listener) { mListener = listener; mRecevier = new InnerRecevier(); } /** * 開始監聽,註冊廣播 */ public void startWatch() { if (mRecevier != null) { mContext.registerReceiver(mRecevier, mFilter); } } /** * 停止監聽,登出廣播 */ 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.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(); }*/ } } } } }
}
2.有的android裝置有物理按鍵。物理按鍵在底層採用的是ADC取樣,根據按鍵按下的電壓值來進行判斷,然後通過input子系統來上報按鍵事件。應用層捕獲如下:
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_F1) {
if (shorpress) {
Log.i(TAG, ” shortpress keycode f1 key up”);
}
shorpress =false;
return true;
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
Log.i(TAG, "long press keycode "+keyCode);
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_F1) {
shorpress = false;
Log.i("taoer", "keycode f1 long press");
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
Log.i(TAG, "short press keycode = "+ keyCode);
if (keyCode == KeyEvent.KEYCODE_F1) {
// Log.i(CmdList.TAG, "KEYCODE_F1");
if (event.getAction() == KeyEvent.ACTION_DOWN) {
event.startTracking();
if (event.getRepeatCount() == 0) {
shorpress = true;
}
}
Log.i("TAG", "keycode f1 short press");
return true;
}
return true;
}