Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

user interface - 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 parts of a service. No activity is involved.

How can I be sure? After I press home on the opened chat screen, it gets minimized back to a chat head, and I can immediately reopen the chat screen. If the chat screen was an activity, then reopening the activity via startActivity(intent) after the home button was pressed, would delay the start of the activity, as specified here: Starting an activity from a service after HOME button pressed without the 5 seconds delay

and here: Reason for 5 sec delay to show an activity on pressing the home button?

in my service onCreate method, i use the following code to display a UI from service:

public class ServiceTest extends Service {
...
    @Override 
    public void onCreate() {
        super.onCreate();

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

        windowManager.addView(someView, params);
    }
....
}

Does anyone have an idea how can I receive the 'home button pressed' event directly from a service displaying a UI? I would like to minimize my view (similar to facebook chat heads) when the user presses the home button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...