Detect 'home button pressed' event in android service displaying a UI (similar to facebook chatheads)

前端 未结 8 1258
暗喜
暗喜 2020-12-13 09:35

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 ar

8条回答
  •  粉色の甜心
    2020-12-13 09:54

    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
                }
            }
        }
    

提交回复
热议问题