Android通过Intent.ACTION_CLOSE_SYSTEM_DIALOGS监听Home按键事件

原文链接:http://www.itnose.net/detail/6115323.html
这个参考了太多,算转载吧,我也不知道怎么样才算原创。比如有些博客参考了别人博客的部分代码,然后另外大部分是自己写的,那算不算原创,,,也罢,目的也只是记录一下,同时调整一下布局,弄的好看点,以便以后参考。

安卓手机中底下都会有三个按钮(魅族奇葩),菜单返回HOME键
返回键用的最多,有独立的方法去监听:onBackPressed
菜单键用的越来越少了,监听:onKeyDown或者onKeyUp都可。
但是HOME键并没有直接的方法去监听,这里使用了广播监听。
原文原话:在每次点击Home按键时都会发出一个action为Intent.ACTION_CLOSE_SYSTEM_DIALOGS的广播,它是关闭系统Dialog的广播,我们可以通过注册它来监听Home按键消息。


工具类

package com.example.qiao.test.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * HOME键监听类。
 */
public class HomeKeyListener extends BroadcastReceiver {
    private Context context;

    public HomeKeyListener(Context context) {
        this.context = context;
    }

    /**
     * 通常在Activity的onStart方法中调用
     */
    public void start() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.registerReceiver(this, filter);
    }

    /**
     * 通常在Activity的onStop方法中调用
     */
    public void stop() {
        context.unregisterReceiver(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
            String reason = intent.getStringExtra("reason");
            if ("homekey".equals(reason)) {
                // 按下HOME健
                if (mOnHomeKeyPressListener != null) {
                    mOnHomeKeyPressListener.onHomeKeyPress();
                }
            } else if ("recentapps".equals(reason)) {
                // 长按HOME键
                if (mOnHomeKeyLongPressListener != null) {
                    mOnHomeKeyLongPressListener.onHomeKeyLongPress();
                }
            }
        }
    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // 按下
    // (这里把 Press 和 LongPress 分开是为了能够使用Lambda)
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    private OnHomeKeyPressListener mOnHomeKeyPressListener;

    public void setOnHomeKeyPressListener(OnHomeKeyPressListener listener) {
        mOnHomeKeyPressListener = listener;
    }

    public interface OnHomeKeyPressListener {
        void onHomeKeyPress();
    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // 长按
    // (长按通常不用。很多手机把长按做成了系统级别的其它功能,比如启动语音助手)
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    private OnHomeKeyLongPressListener mOnHomeKeyLongPressListener;

    public void setOnHomekeyLongPressListener(OnHomeKeyLongPressListener listener) {
        mOnHomeKeyLongPressListener = listener;
    }

    public interface OnHomeKeyLongPressListener {
        void onHomeKeyLongPress();
    }
}

使用例子

package com.example.qiao.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import com.example.qiao.test.utils.HomeKeyListener;

public class MainActivity extends Activity {
    private Context context;
    private HomeKeyListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.context = this;

        listener = new HomeKeyListener(this);

        listener.setOnHomeKeyPressListener(() -> {
            Toast.makeText(context, "按下了HOME键", Toast.LENGTH_SHORT).show();
        });

        listener.setOnHomekeyLongPressListener(() -> {
            Toast.makeText(context, "长按了HOME键", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected void onStart() {
        listener.start();
        super.onStart();
    }

    @Override
    protected void onStop() {
        listener.stop();
        super.onStop();
    }
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值