当前位置:操作系统 > 安卓/Android >>

ANDROID 返回,菜单和HOME键的监听

,返回和菜单键是可以直接重写onKeyDown(int keyCode, KeyEvent event) 方法监听:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { // 监控/拦截/屏蔽返回键
//do something
} else if (keyCode == KeyEvent.KEYCODE_MENU) {

//do something
} else if (keyCode == KeyEvent.KEYCODE_HOME) {

//这里操作是没有返回结果的
}
return super.onKeyDown(keyCode, event);
}


在do something 中,如果返回false则时间不继续传递,也就是不会触发相应的后续事件

2,如果是Home事件,看网上的说法,是底层拦截了这个事件,但是拦截后会发出一个广播。所以注册个广播接收HOME按键事件就行了。

/**
 * Home键监听封装
 *
 *
 */
public class HomeListener {
 
    static final String TAG = "HomeListener";
    private Context mContext;
    private IntentFilter mFilter;
    private OnHomePressedListener mListener;
    private InnerRecevier mRecevier;
 
    // 回调接口 
    public inte易做图ce OnHomePressedListener {
        public void onHomePressed();
 
        public void onHomeLongPressed();
    }
 
    public HomeListener(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();
                        }
                    }
                }
            }
        }
    }
}


在使用的时候:

 


private HomeListener mHomeWatcher;


在oncreate()中:

mHomeWatcher = new HomeListener(this);
        mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
            @Override
            public void onHomePressed() {
            if(null!=vibrator){
    vibrator.cancel();
    }
    if(null!=timer){
    timer.cancel();
    }
            }
            @Override
            public void onHomeLongPressed() {
            if(null!=vibrator){
    vibrator.cancel();
    }
    if(null!=timer){
    timer.cancel();
  &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,