带覆盖的服务-按下后退按钮 [英] Service with Overlay - catch back button press

查看:113
本文介绍了带覆盖的服务-按下后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做?

当前解决方案

我启动了一个透明的活动,该活动会捕获后按,然后将其转发给我的服务并随后自行关闭.但是此活动将在当前正在运行的活动中显示,因此它不是一个非常漂亮的解决方案.

I launch a transparent activity, that catches the back press, forwards it to my service and closes itself afterwards. But this activity will be visible in the current running activities and therefore it's not a very beautiful solution.

看到的解决方案

我见过一个在服务中确实捕捉到后按的应用程序-却没有活动可以捕捉到后按的活动.如果我显示当前正在运行的活动,则此应用程序无任何作用.

I've seen an app that does catch the back press in a service - without an activity that catches the back press. If I show the current running activities, there is nothing from this app.

问题

这怎么办?我读过很多线程说这是不可能的,但是我可以看到,有些应用程序以某种方式实现了这一目标...

How can this be done? I've read so many threads that say, this is not possible, but I can see, that there are apps that achieve that somehow...

推荐答案

我想我了解了它的工作原理...不要在叠加层视图中使用WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE +覆盖视图的dispatchKeyEvent:

I think I found out how it works... DON'T USE WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE in your overlays views + overwrite the dispatchKeyEvent of your view:

 @Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
    {
        // handle back press
        // if (event.getAction() == KeyEvent.ACTION_DOWN)
        return true;
    }
    return super.dispatchKeyEvent(event);
}

这篇关于带覆盖的服务-按下后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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