How to detect recent button in android 8.1.0

1.1k views Asked by At

I want to detect recent button but in android 8.1.0 it's not working.Below code is working on another version of android but in 8.1.0 the Intent.ACTION_CLOSE_SYSTEM_DIALOGS broadcast is not calling.I am using below implementation.

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();
    mFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    mFilter.addAction("");
}

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) {
                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_RECENT_APPS)) {
                        mListener.onHomeLongPressed();
                    }
                }
            }
        }
    }
}

}

and in class, I am calling using below code.

HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.startWatch();

Please help!.

Edited---- The above code is working properly in normal flow but when the screen pinning is set(ON) then it's not working. Even i am not getting any event like KeyUp, KeyDown

3

There are 3 answers

1
Akshay Kumar S On

com.android.systemui package will be the foreground app when you click recent app button, so find the foreground running apps and launch your page if foreground running app is 'com.android.systemui'.

2
Android dev On

Please use below code

 `@Override
  public boolean dispatchKeyEvent(KeyEvent event) {
      Log.i("key pressed", String.valueOf(event.getKeyCode()));
      return super.dispatchKeyEvent(event);
  }`
0
Daksh Rawal On
HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
@Override
public void onHomePressed() {
    // do something here...
    }
@Override
public void onHomeLongPressed() {
    }
});
mHomeWatcher.startWatch();

For a detailed Answer Check

Detect Home And Recent App Button