Android开发中如何监听用户的home键

在日常开发中,监听用户返回的操作比较常见,直接在onKeyDown中判断KeyEvent.ACTION_DOWN直接就可以获取相应的监听,但是如何获取到用户对home键的监听呢?正巧在项目中遇到了一个实际应用场景,就很自然的也去KeyEvent里面去找,别说还真有哈,KEYCODE_HOME对应的就是home键的点击事件,但是跑到手机上怎么都没有实现!!!!哈哈哈,原来,原因在这里:
/** 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;
看到了吧? 这个事件直接被framework层给截获了,never delivered!!永远不会传递的application层!!!!那我们怎么才能获取到home键的监听呢???google还是给开发者提供了一扇窗滴!广播~
当用户点击系统层级的按键的时候都会发出系统广播,这个时候我们只要register相应的广播就可以获取当相应事件了,需要注意的是,静态注册不可以~~只能在代码里面动态注册!闲话说了那么多直接上代码,我是在application里面直接注册,这样在整个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;

    // 回调接口
    public interface OnHomePressedListener {
        public void onHomePressed();

        public void onHomeLongPressed();
    }

    public HomeKeyListener(Context context) {
        mContext = context;
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    }

    /**
     * 设置监听
     *
     * @param listener
     */
    public void setOnHomePressedListener(OnHomePressedListener listener) {
        mListener = listener;
        mRecevier = new HomeKeyRecevier();
    }

    /**
     * 开始监听,注册广播
     */
    public void startWatch() {
        if (mRecevier != null) {
            mContext.registerReceiver(mRecevier, mFilter);
        }
    }

    /**
     * 停止监听,注销广播
     */
    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();
                        }
                    }
                }
            }
        }
    }
}
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();

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值