Android基础篇 监听Home键

Posted 其子昱舟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android基础篇 监听Home键相关的知识,希望对你有一定的参考价值。

一、Home键监听 工具类

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

import com.blankj.utilcode.util.LogUtils;

public class HomeWatcher 

    private static final String TAG = "HomeWatcher";
    private Context mContext;
    private IntentFilter mFilter;
    private OnHomePressedListener mListener;
    private InnerRecevier 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) 
                    LogUtils.i("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();
                        
                    
                
            
        
    

二、使用

HomeWatcher mHomeWatcher = new HomeWatcher(this); 
        mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() 
            @Override 
            public void onHomePressed()   
                //按了HOME键 
             
            @Override 
            public void onHomeLongPressed() 
                //长按HOME键
             
        ); 
        mHomeWatcher.startWatch();

三、程序销毁时(如:onDestroy)时添加如下代码:

if(mHomeWatcher != null)
        mHomeWatcher.stopWatch();// 在销毁时停止监听,不然会报错的。 

以上是关于Android基础篇 监听Home键的主要内容,如果未能解决你的问题,请参考以下文章

Android 进阶Android Home 键监听

Android监听home键

Android Back Home键监听

Android Back Home键监听

Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听

iOS 监听Home键及锁屏键