9

I am building a app which starts a service on some event. The service have a overlay view on top of every app (like facebook chat head). I want to stop the service and remove the overlay view on click of navigation soft key buttons (back, home and recent apps).

I have successfully made an overlay view by extending RelativeLayout which is working totally fine. I am following this for overlay view.

The same thing is Facebook doing for expansion and closing of chat head. Thanks in advance. :)

EDIT

My problem is same as stated here.

Community
  • 1
  • 1
Ajeet
  • 1,540
  • 1
  • 17
  • 30

2 Answers2

10

I faced the same problem as you did. So after researching some days I found below class for detecting (pressed event of home and recentapps keys).

public class HardwareKeyWatcher {
    static final String TAG = "HardwareKeyWatcher";
    private Context mContext;
    private IntentFilter mFilter;
    private OnHardwareKeysPressedListener mListener;
    private InnerReceiver mReceiver;


    public interface OnHardwareKeysPressedListener {
        public void onHomePressed();

        public void onRecentAppsPressed();
    }

    public HardwareKeyWatcher(Context context) {
        mContext = context;
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        mFilter.setPriority(1000);
    }

    public void setOnHardwareKeysPressedListenerListener(OnHardwareKeysPressedListener 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_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.onRecentAppsPressed();
                        }
                    }
                }
            }
        }
    }
}

Use above class as below in onCreate method in your service.

@Override
public void onCreate() {
 mHardwareKeyWatcher = new HardwareKeyWatcher(this);
        mHardwareKeyWatcher.setOnHardwareKeysPressedListenerListener(new HardwareKeyWatcher.OnHardwareKeysPressedListener() {
            @Override
            public void onHomePressed() {
                System.out.println("stop your service here");
            }

            @Override
            public void onRecentAppsPressed() {
                System.out.println("stop your service here");
            }
        });
 mHardwareKeyWatcher.startWatch();
}

and for detecting back press you can follow this link(Service with Overlay - catch back button press). I tested this solution and it worked for me.

So you can call stopService in onHomePressed() and onRecentAppsPressed() methods and then override onDestroy() method in your service class as below.

@Override
public void onDestroy() {
  super.onDestroy();
  mWindowManager.removeView(mYourOverlayView);
  mHardwareKeyWatcher.stopWatch();
}

Please try these things and let us know... good luck.

Community
  • 1
  • 1
Tharindu Welagedara
  • 2,660
  • 18
  • 27
2

I think Facebook uses Activity. Probably When You click Floating view, you go to transparent activity.

takahirom
  • 1,934
  • 2
  • 12
  • 21
  • 2
    Yes, but the problem with activity is that it get delayed by 5 second http://stackoverflow.com/questions/5600084/starting-an-activity-from-a-service-after-home-button-pressed-without-the-5-seco – Ajeet Jan 04 '16 at 03:45
  • Please try using redirect Activity. Like this Floating view ->redirect Activity -> Activity. – takahirom Jan 04 '16 at 03:58
  • Can you please elaborate your solution of redirecting activity? Thanks – Ajeet Jan 04 '16 at 04:06
  • Redirecting activity is very simple. please put `startActivity()` and `finish()` to `onCreate()` in redirecting activity. – takahirom Jan 04 '16 at 04:15
  • How redirecting an Activity could help can you explain? As starting the first activity may take 5 sec to start. – Ajeet Jan 04 '16 at 04:23