安卓:覆盖在窗口在任务的活动 [英] Android: Overlay on Window over Activities of a Task

查看:130
本文介绍了安卓:覆盖在窗口在任务的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造一个覆盖,就像一个平视显示器,期间驻留在屏幕上 我的应用程序的活动栈(我的应用程序的任务)的变化。

I wanted to create an Overlay, like a HUD, that resides on the screen during my applications activity stack (the task of my app) changes.

我发现一对夫妇使用窗口管理器的例子,但我不能想出的参数进行正确的z-index,如果你想要的。它要么到下一个活动疲软会去ontop我的叠加或强覆盖是一个系统范围的覆盖,这是也是可见的,当应用程序移动到后台。

I found a couple of examples using WindowManager but I couldn't figure out the parameterization for the correct z-index if you want. It was either to weak the next activity would go ontop of my overlay or to strong the overlay was a system wide overlay that was visible also when the app moved into the background.

我瞄准在属于我的应用程序或任务一切活动都顶部显示一个视图(app将是preferred)。我知道,应用程序和任务是两个不同的东西在Android上...

I aim for displaying a view on top of all activites belonging to my app or task (app would be preferred). I am aware that apps and tasks are two different things on android...

我明确不希望的就是使用需要android.permission.SYSTEM_ALERT_WINDOW全系统窗口

What I explicitly don't want is to use system wide windows that require android.permission.SYSTEM_ALERT_WINDOW

---我用例 -

--- my usecase --

我在执行一个流程,包括活动提供用户输入的表单。该用户输入具有以相当复杂的方式进行处理,得到为+/- 10状态的可能的结果。处理最多需要10分钟,具体取决于进程的结果我想显示相应的视图。虽然进程运行我的意图,以保持用户的更新,但不能让他浏览应用程序(除了中止的过程)。长时间运行的操作的每一个可能的结果将是psented在不同的活动$ P $。

I am implementing a flow that includes an activity providing a form for user input. that user input has to be processed in a rather complex manner, yielding a possible outcome of +/- 10 states. The processing can take up to 10 minutes and depending on the outcome of that process I want to display a corresponding view. While the process runs I intent to keep the user updated but can not allow him to navigate the app (except aborting the process). Every possible outcome of the long running operation will be presented in a different activity.

我清楚地知道,有几个可能的办法(例如只有一个活动)。但是,这一决定已经作出,并超出了这个问题的范围。我已经实现了使用系统的Windows显示了覆盖在一个解决方案。对于隐藏覆盖我不得不ONSTART算,的onStop事件和跨preT应用程序没有移动到后台和应用程序并进入前景。这种感觉很脏,我并不感到满意的解决方案。我宁愿退一步,并显示我的叠加在呼叫活动的顶部和后整理的过程中隐藏,并前进到活动中显示的结果。

I am well aware, that there are several approaches possible (for example having one activity only). But that decision has already been made and is out of scope of that question. I have implemented a solution that uses System Windows to display that overlay in. For hiding the overlay I have to count onStart, onStop events and interpret "App did to move into background" and "App did move into foreground". This feels dirty and I am not satisfied with that solution. I'd rather take a step back and display my overlay on top of the calling activity and upon finishing the process hiding it and moving forward to the activity displaying the result.

还有一件事我想是从一个活动移动视图到另一个。但是,这显示了一些闪烁和我的动画中断,我不喜欢。

One more thing I tried is to move a view from one activity to another. But this shows some flickering and interruption of my animation that I don't like.

我会AP preciate,如果我们可以把重点放在了其是否可以显示在应用程序/任务窗口的顶部,而不是一个系统或活动窗口内的视图的问题;)

I would appreciate it if we could focus on the question of whether its possible to display a view on top of the application/task window rather than inside a system or activity window ;)

推荐答案

您真的只有两个选择。

1)您可以创建Theme.Dialog为主题的活动。这将显示在你的窗口顶部的弹出窗口。您可以创建对话框是模态少(可以通过点击)。在我的快速测试我是不是能够得到叠加到我的屏幕的边缘,虽然可能修改主题将解决这个问题。

1) You can create an activity with the theme of Theme.Dialog. This will display a popup on top of your window. You can create the dialog to be modal-less (can click through). In my quick testing I wasn't able to get overlay to the edges of my screen although maybe modifying the theme would fix that.

MainActivity.java

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button test = (Button) this.findViewById(R.id.test);
        test.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Button) v).setBackgroundColor(Color.RED);
            }
        });

        Intent i = new Intent(this, SecondActivity.class);
        startActivity(i);
    }
}

SecondActivity.java

SecondActivity.java

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_main);

        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        window.setGravity(Gravity.BOTTOM);
    }

    @Override
    public void onBackPressed() {
            //Override to prevent back button from closing the second activity dialog
    }
}

清单

    ....
    <activity
        android:name="com.example.control.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.control.SecondActivity"
        android:theme="@android:style/Theme.Dialog"
        android:label="@string/app_name" >
        ....
    </activity>
    ....

2)第二个选择是使用一个SYSTEM_ALERT_WINDOW。我preFER这种方法显著多。你是正确的,它可以是可见的,在所有其他应用程序之上,但是,你可以控制它是可见的,当它不是。我不会发表任何来源$ C ​​$ C,但我会给你攻击的总体规划。

2) The second option is to use a SYSTEM_ALERT_WINDOW. I prefer this method significantly more. You are correct that it CAN be visible and on top of every other app, however, you can control when it is visible and when it isn't. I'm not going to post any source code but I will give you a general plan of attack.

在创建服务,使用AIDL绑定到它。你就可以直接与服务交谈,告诉它时,隐藏和秀的覆盖这种方式。说到隐藏和显示的,在onPause和onResume可以用来告诉隐藏和显示覆盖的服务。最后,如​​果你需要在收到您的覆盖click事件,这将被证明是非常棘手的触摸事件不总是充当你所期望的方式。

When you create the service, bind to it using an AIDL. This way you'll be able to talk directly with the service to tell it when to 'hide' and 'show' the overlay. Speaking of hiding and showing, onPause and onResume can be used to tell the service to hide and show the overlay. Lastly, if you need to receive click events on your overlay, that will prove to be tricky as the touch events don't always act the way you expect them to.

好运。

这篇关于安卓:覆盖在窗口在任务的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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