【问题标题】:How to detect if it is Recents button, Multi Window mode or home button onUserLeaveHint()如何检测它是最近按钮、多窗口模式还是主页按钮 onUserLeaveHint()
【发布时间】:2017-02-12 18:17:55
【问题描述】:

在我的应用程序中,当用户按下主页按钮时,我依赖 onUserLeaveHint() 方法,但是当您在 android 7.0 中通过长按“最近按钮”(我不想执行与按下主页按钮时相同的操作)。所以我想知道是否有办法检测哪个是哪个。干杯!

注意: onMultiWindowModeChanged() 被调用 onUserLeaveHint()

【问题讨论】:

  • 如果它在 7.0 中没有被弃用,它仍然可以工作
  • 它仍然有效,但它不能帮助我实现我的目的。我不希望在用户激活多窗口模式时调用它。
  • 即使尚未调用 onMultiWindowModeChanged(),从 onUserLeaveHint() 调用 isInMultiWindowMode() 会返回什么
  • 它将当前模式返回为布尔值,我们当时不知道是切换到多窗口模式还是单窗口模式。 @Scrotos

标签: java android android-7.0-nougat multi-window


【解决方案1】:

我想这就是你要找的。

HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
    @Override
    public void onHomePressed() {
        // do something here...
    }
    @Override
    public void onHomeLongPressed() {
    }
});
mHomeWatcher.startWatch();
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class HomeWatcher {

    static final String TAG = "hg";
    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_LONG_PRESS = "assist";
        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)) {
                            mListener.onHomePressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_LONG_PRESS)) {
                            mListener.onHomeLongPressed();
                        }
                    }
                }
            }
        }
    }
}



public interface OnHomePressedListener {
    public void onHomePressed();

    public void onHomeLongPressed();
}

【讨论】:

  • 只有当我有一个主屏幕活动(“android.intent.category.HOME”)并且你的答案是这个问题的第一个答案的复制粘贴时,我才能听主键:@987654321 @您应该分享答案链接以给予信用而不是复制粘贴。我不能接受这个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 2016-05-31
  • 2012-05-06
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
相关资源
最近更新 更多