How to determine if there is a Repeat button, Multi-window mode, or Home button onUserLeaveHint ()

In my application, I rely on the onUserLeaveHint() method when the user clicks the home button, but this method is also called when you start multi-window mode in android 7.0 by long pressing the "recents" button (that I don’t want to do the same thing as when you press the home button). So I want to know if there is a way to discover what is. Hooray!

Note: onMultiWindowModeChanged() is called after onUserLeaveHint()

+6
source share
1 answer

I think this is what you are looking for.

 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(); } 
0
source

All Articles