Detect 'home button pressed' event in andr

2019-01-21 22:54发布

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.

8条回答
Ridiculous、
2楼-- · 2019-01-21 23:23

I'm fairly sure facebook doesn't listen for home button presses, because their logic requires the chat heads to show up regardless of what app is visible, as long as it isn't facebook.

A simple and crude way to do this would be to maintain a static flag visible in your Application singleton, and modify it from every Activity you have. In the onPause(), set it to false, and in onResume() and onCreate() set it to true.

Then simply check the state of this flag, and act accordingly. If its true, it means your app is visible.

You might want to add a small pause before you act on it to prevent your injected Views from flickering each time an Activity is changed.

查看更多
劫难
3楼-- · 2019-01-21 23:28

This is my way. It work fine. Put it in onReceive() function.

if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
    {
        String reason = intent.getStringExtra(SYSTEM_REASON);

        //Toast.makeText(context,"ACTION_CLOSE_SYSTEM_DIALOGS : Reason : " + reason ,Toast.LENGTH_LONG).show();

        // Detect home screen key press or "recent app" key pressed when screen is in unlocked state
        if (reason != null)
        {
            if (reason.equals(SYSTEM_HOME_KEY))
            {
            // For Home press
            }
            else if (reason.equals(SYSTEM_RECENT_APPS))
            {
            // For long press
            }
        }
    }
查看更多
登录 后发表回答