Android 独立透明 Activity

发布于 12-05 08:59 字数 2261 浏览 0 评论 0原文

我的应用程序的主要活动是首选项页面。这是用户单击应用程序图标时显示的内容。我还有一项服务可以发送用户状态栏通知,并可以在屏幕上显示半透明的覆盖层。我按照这篇帖子创建了我的透明活动,以及所有这些作品。

问题是,每当我显示半透明活动时,应用程序的主窗口(首选项页面)在其后面可见。这意味着半透明覆盖层出现在当前正在运行的任何其他应用程序之上。

如何才能使半透明活动出现时,我的应用程序中的其他活动不可见?

这是我在 AndroidManifest.xml 中的主要活动定义:

<activity android:name=".AppPreferences" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

我还有半透明覆盖层,它使用派生自 Theme.Translucent 的自定义主题:

<activity android:name=".AppPopupActivity" android:theme="@style/Theme.SemiTransparent">
  <intent-filter>
        <action android:name="com.app.HIDE_POPUP"></action>
    </intent-filter>
</activity>

这是半透明覆盖层的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <RelativeLayout android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true" >

        <Button android:text="@string/button_done"
                android:id="@+id/doneButton"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
        </Button>
    </RelativeLayout>
</RelativeLayout>

和服务:

<service android:name="AppService">
  <intent-filter>
    <action android:name="com.app.AppService" />
  </intent-filter>
</service>

要显示透明活动,我运行以下命令服务中:

Intent intent = new Intent(this, AppPopupActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);

感谢您的帮助

My application's main activity is the preferences page. This is what gets displayed when the user clicks the application icon. I also have a service which sends the user status bar notifications, and can display a translucent overlay on the screen. I followed this post to create my transparent activity, and all of this works.

The problem is that ever time I display my translucent activity, the application's main window (the preferences page) is visible behind it. Meaning, that the translucent overlay appears on top of whatever other application is currently running.

How can I make so that when the translucent activity appears, no other activity from my application is visible?

This is my main activity's definition in AndroidManifest.xml:

<activity android:name=".AppPreferences" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

I also have translucent overlay which uses a custom theme which derives from Theme.Translucent:

<activity android:name=".AppPopupActivity" android:theme="@style/Theme.SemiTransparent">
  <intent-filter>
        <action android:name="com.app.HIDE_POPUP"></action>
    </intent-filter>
</activity>

Here is the layout for the translucent overlay:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <RelativeLayout android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true" >

        <Button android:text="@string/button_done"
                android:id="@+id/doneButton"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
        </Button>
    </RelativeLayout>
</RelativeLayout>

And the service:

<service android:name="AppService">
  <intent-filter>
    <action android:name="com.app.AppService" />
  </intent-filter>
</service>

To display the transparent activity I run the following in the service:

Intent intent = new Intent(this, AppPopupActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);

Thanks you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

血之狂魔2024-12-12 08:59:58

尽管Profete162的回答没有起作用,但它引导我走向了正确的方向。经过更多的阅读和实验,我相信正确的答案是将主活动的 launchMode 更改为“singleInstance”,如下所示:

<activity android:name=".AppPreferences" android:label="@string/app_name"
          android:launchMode="singleInstance">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

Although the answer by Profete162 bellow did not work, it led me in the right direction. After more reading and experimentation, I believe the right answer is to change the main activity's launchMode to "singleInstance" as follows:

<activity android:name=".AppPreferences" android:label="@string/app_name"
          android:launchMode="singleInstance">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
还在原地等你2024-12-12 08:59:58

“问题是,每当我显示半透明活动时,应用程序的主窗口(首选项页面)就在其后面可见。”

您的问题是您有设置活动,并且在其之上,透明活动。它们位于“堆栈”中

当您调用通知时,transparentActivity 位于活动堆栈前面(= 设置)。

如果您想查看透明活动“后面”发生的情况,则必须像这样删除堆栈:

尝试添加 FLAG_ACTIVITY_CLEAR_TOP:

这种启动模式也可以与
FLAG_ACTIVITY_NEW_TASK:如果用于启动任务的根活动,
它将将该任务的任何当前正在运行的实例带到
前景,然后将其清除到根状态。这尤其是
有用,例如,从通知启动活动时
经理。

所以启动 A 的代码是:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

"The problem is that ever time I display my translucent activity, the application's main window (the preferences page) is visible behind it."

Your issue is that you have your settings activity and on top of it, the TransparentACtivity. They are in a "stack"

When you call your notification, the transparentActivity comes in front of the activity stack (= settings)

If you want to see what happens "behind" the transparentACtivity, you have to get rid of your stack like this:

Try adding FLAG_ACTIVITY_CLEAR_TOP:

This launch mode can also be used to good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
it will bring any currently running instance of that task to the
foreground, and then clear it to its root state. This is especially
useful, for example, when launching an activity from the notification
manager.

So your code to launch A would be:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文