`
yhz61010
  • 浏览: 550657 次
  • 来自: -
博客专栏
63c13ecc-ef01-31cf-984e-de461c7dfde8
libgdx 游戏开发
浏览量:11959
社区版块
存档分类
最新评论

[转] Detect home button press in android

阅读更多
原文地址:https://stackoverflow.com/a/27956263/6091500

亲测可用。
测试用机: 小米 2S Android 5.0

HomeWatcher mHomeWatcher = new HomeWatcher(this);  
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {  
    @Override  
    public void onHomePressed() {  
        ALog.i(TAG, "***** ===== onHomePressed() ===== *****");  
    }  

    @Override  
    public void onHomeLongPressed() {  
        ALog.i(TAG, "***** ===== onHomeLongPressed() ===== *****");  
    }  
});  
mHomeWatcher.startWatch();  

public interface OnHomePressedListener {
    public void onHomePressed();

    public void onHomeLongPressed();
}

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

/**
 * Created by Michael Leo <y@ho1ho.com>
 * Date: 2017/07/20 17:38
 */
public class HomeWatcher {
    private static final String TAG = HomeWatcher.class.getSimpleName();
    private Context mContext;
    private IntentFilter mFilter;
    private OnHomePressedListener mListener;
    private InnerRecevier mRecevier;

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

    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";
        final String SYSTEM_DIALOG_REASON_LONG_PRESS = "assist";
        final String SYSTEM_DIALOG_REASON_VOICE_INTERACTION = "voiceinteraction";

        @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) {
                    ALog.e(TAG, "action:" + action + ",reason:" + reason);
                    if (mListener != null) {
                        if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                            mListener.onHomePressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                            mListener.onHomeLongPressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_LONG_PRESS)) {
                            mListener.onHomeLongPressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_VOICE_INTERACTION)) {
                            mListener.onHomeLongPressed();
                        }
                    }
                }
            }
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics