Android捕获home按键 以及实体键捕获

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;
}

你可能感兴趣的:(app)