0

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

Android dev
  • 273
  • 2
  • 5
  • 23

3 Answers3

0

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'.

Akshay Kumar S
  • 333
  • 3
  • 12
0
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

Daksh Rawal
  • 238
  • 2
  • 13
-1

Please use below code

 `@Override
  public boolean dispatchKeyEvent(KeyEvent event) {
      Log.i("key pressed", String.valueOf(event.getKeyCode()));
      return super.dispatchKeyEvent(event);
  }`
Android dev
  • 273
  • 2
  • 5
  • 23