在显示 UI 的 android 服务中检测“按下主页按钮"事件(类似于 Facebook 聊天头) [英] Detect 'home button pressed' event in android service displaying a UI (similar to facebook chatheads)

查看:17
本文介绍了在显示 UI 的 android 服务中检测“按下主页按钮"事件(类似于 Facebook 聊天头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Facebook 聊天头中,这是 facebook 信使应用程序的一部分,我注意到以下行为:据我所知,聊天头本身和打开的聊天屏幕都是服务的一部分.不涉及任何活动.

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.

我怎么确定?在打开的聊天屏幕上按 home 键后,它会最小化回聊天头,我可以立即重新打开聊天屏幕.如果聊天屏幕是一个活动,那么在按下主页按钮后通过 startActivity(intent) 重新打开活动将延迟活动的开始,如下所示:从按下 HOME 键后没有 5 秒延迟的服务

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

这里:按下主页按钮显示活动延迟 5 秒的原因?

在我的服务 onCreate 方法中,我使用以下代码显示来自服务的 UI:

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);
    }
....
}

有谁知道如何直接从显示 UI 的服务接收按下主页按钮"事件?当用户按下主页按钮时,我想最小化我的视图(类似于 Facebook 聊天头).

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.

推荐答案

在对这个问题进行了自己的研究之后,我得出了以下结论:

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

Facebook 通过提供标志来拦截"导航按钮TYPE_SYSTEM_OVERLAY 属性.查看@jawsware 提供的这篇文章答案

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.

使用 onFocusChangedListener 视图回调或 OnKey 侦听器 他们通过关闭覆盖的全屏模式对其做出反应.

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

这也解释了他们如何通过 3 个导航按钮做出反应 - 主页/返回/最近的任务

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

这篇关于在显示 UI 的 android 服务中检测“按下主页按钮"事件(类似于 Facebook 聊天头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆