Friday, February 22, 2019

Detect button home click

To detect button home click, we can use onWindowFocusChanged to know app lose focus, or we create a function like this.
public class HomeWatcher {
static final String TAG = "hg";
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private InnerReceiver mReceiver;
public HomeWatcher(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}

public void setOnHomePressedListener(OnHomePressedListener listener) {
      mListener = listener;
      mReceiver = new InnerReceiver();
}

public void startWatch() {
      if (mReceiver != null) {
            mContext.registerReceiver(mReceiver, mFilter);
      }
}

public void stopWatch() {
      if (mReceiver != null) {
            mContext.unregisterReceiver(mReceiver);
      }
}
class InnerReceiver 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();
                                    }
                              }
                        }
                       
                  }
            }
      }
}
In main class, declare variable.
HomeWatcher mHomeWatcher = new HomeWatcher(this);
Copy this function in to above last close bracket.
public interface OnHomePressedListener {
          void onHomePressed();
          void onHomeLongPressed();
   }
Copy in to onCreate().
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
     @Override
public void onHomePressed() {
               finish();
Intent intent = new Intent(getBaseContext(), mainclass.class);
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)  
     startActivity(intent);
               }
               @Override
               public void onHomeLongPressed() {
               }
          });
mHomeWatcher.startWatch();

Here we reopen main class when user press Home button, you can change if want to do something else.
Last, add this to stop home watch when exit app.
@Override
     protected void onDestroy() {
          mHomeWatcher.stopWatch();
          super.onDestroy();
   }
If you detect Home button like this.
@Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
    
      if ((keyCode == KeyEvent.KEYCODE_HOME)) {

      return true;
      }
      if ((keyCode == KeyEvent.KEYCODE_BACK)) {
         
           return true;
           }
      if ((keyCode == KeyEvent.KEYCODE_MENU)) {
           
           return true;
           }
      return super.onKeyDown(keyCode, event);
    }
This way, Home button will has no react when pressed. If you add this.
@Override
public void onPause() {
super.onPause();
//Do smt
}
Now Home button can detected, but command will happen the same if you press Menu or Back button. You must set return false; in KEYCODE_MENU and add more count variables to distinguish.

Use HomeWatcher is the best way, only Home button react when pressed.

No comments:

Post a Comment