Skip to content Skip to sidebar Skip to footer

Detect 'home Button Pressed' Event In Android Service Displaying A Ui (similar To Facebook Chatheads)

In facebook chatheads, that are part of the facebook messenger app, I noticed the following behavior: As far as I can see, the chat head itself and the opened chat screen are all p

Solution 1:

well after doing my own research about that issue, I've came with the following conclusions:

Facebook "intercepting" the navigation buttons by providing the flag TYPE_SYSTEM_OVERLAY attribute. look on this post answer provided by @jawsware

doing what he advices not to do - will lead to the "affect" of controling the navigation buttons

using this flag provides focus on your overlay, and takes the focus from the activity behind.

with the onFocusChangedListener view callback or the OnKey listener they reacts to it with closing the full screen mode of the overlay.

that's also explains how they reacts to it from the 3 navigation buttons - home / back / recent tasks

Solution 2:

You can achieve it by overriding View.onCloseSystemDialogs(). You may need to check View.java because this method is not visible in API doc.

Obviously, we can not detect 'Home button pressed' in general. In most cases, the window is created by startActivity(). And CloseSystemDialogs routine, which is caused by 'Home button pressed', is handled by internal class of android framework, PhoneWindow.DecorView as a view ancestor of ViewRootImpl. But when we add views to window manager directly, view ancestor of ViewRootImpl is being our custom view but PhoneWindow.DecorView. Checking the source code of ViewRootImpl.java might be easier to understand.

KeyEvent.KEYCODE_APP_SWITCH causes closeSystemDialogs routine, too. This explains how facebook chatheads works.

Solution 3:

ok complete edit of my answer,

Override below method in your activity.

@OverridepublicvoidonAttachedToWindow() {
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
    }

After overriding above method, now you can easily listen HOME Key press in your activity using onKeyDown() method.

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {     

        if(keyCode == KeyEvent.KEYCODE_HOME)
        {
           //The Code Want to Perform. 
        }
    });

try this hope it helps

Solution 4:

You can monitor (e.g. check every 200 ms) the top running activity, and see if it is your activity or some other activity, and know when it changes. This will also let you handle cases like an incoming call.

ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
am.getRunningTasks(1).get(0).topActivity...

Solution 5:

Here is working sample code.

mLinear =  new LinearLayout(getApplicationContext()) {

          //home or recent buttonpublicvoidonCloseSystemDialogs(String reason) {
              //The Code Want to Perform. 
          }

          @Override
          public boolean dispatchKeyEvent(KeyEvent event) {
              if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
                || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN
                || event.getKeyCode() == KeyEvent.KEYCODE_CAMERA ) {

              //The Code Want to Perform.
              }
          return super.dispatchKeyEvent(event);
          }
};

mLinear.setFocusable(true);

View mView = inflate.inflate(R.layout.floating_panel_layout, mLinear);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

//params
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
             width,
             height,
             WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
             WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
              PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
wm.addView(mView, params);

Post a Comment for "Detect 'home Button Pressed' Event In Android Service Displaying A Ui (similar To Facebook Chatheads)"