在后台显示另一个应用程序上的对话框活动 [英] Show dialog activity over another app from background

查看:205
本文介绍了在后台显示另一个应用程序上的对话框活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个应用程序A,它可以打开另一个不受您控制的应用程序B(例如地图)(即它是一个已存在的应用程序).因此,现在应用程序A在后台.假设发生了一个事件,并且A希望在应用B的UI上显示一个浮动对话框(同时在应用B的活动后面保持可见).这可能吗?

Say you have an app A which opens up another app B (e.g. a map), which is not controlled by you (i.e. it's a preexisting app). So now app A is in the background. Suppose an event occurs and A wants to show a floating dialog over app B's UI (while leaving app B's activity visible behind it). Is this possible?

(通常的回答是显示通知,但这不是大众市场应用,我们正试图直接吸引用户的注意力.)

(The usual answer to this would be to display a notification, but this is not a mass market app, and we are trying to get the user's attention very directly.)

目前,我正在尝试执行以下操作:

Currently, I was trying to do something like this:

// This code runs in a class other than app A's main activity,
// and the "activity" variable used here is a reference to that activity.
Intent intent = new Intent(activity, NotificationDialogActivity.class);
// EDIT: I'm not exactly sure whether NEW_TASK helps here or not
// so I removed it, but the REORDER_TO_FRONT would ideally cause
// app A's dialog activity to be moved to the front of the back stack?
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// The "msg" variable here is just some data being passed to the dialog activity
// I included it here only so it is clear that there is a purpose.
intent.putExtra(NotificationDialogActivity.EXTRA_MSG, msg);
activity.startActivity(intent);

在应用A中(背景中的一个).

from within app A (the one in the background).

但是,当我这样做时,会发生对话框是在原始app A活动和back B堆栈中的app B活动之间插入 .

But what happens when I do that is that the dialog gets inserted between the original app A activity and the app B activity on the back stack.

推荐答案

为了使对话框活动显示在另一个应用程序上,必须做一些事情:

In order to have a dialog activity shown over another application, a few things must be done:

  • 对话框活动必须具有半透明的主题,以允许其他应用程序显示在其后面(请参见@android:style/Theme.Translucent.NoTitleBar)
  • 出于相同的原因,对话框活动一定不能填满屏幕(请参见Window.setLayout)
  • 对话框活动必须是与基本活动分开的任务的一部分,这样,当对话框活动显示在另一个应用程序上方时,它也不会将基本活动也拉到另一个应用程序上方(请参见FLAG_ACTIVITY_NEW_TASK)
  • 对话框活动必须放在最前面,以便从中启动它的活动仍在后台运行(请参阅FLAG_ACTIVITY_REORDER_TO_FRONT)
  • 对话框活动必须以某种方式显示对话框,例如通过创建直接扩展Dialog类的类
  • The dialog activity must have a translucent theme, to allow the other application to show up behind it (see @android:style/Theme.Translucent.NoTitleBar)
  • The dialog activity must not fill the screen, for the same reason (see Window.setLayout)
  • The dialog activity must be part of a separate task from the base activity, so that it when it is shown above the other application, it does not pull the base activity above the other application as well (see FLAG_ACTIVITY_NEW_TASK)
  • The dialog activity must be brought to the front, so that it is still shown if the activity it is being launched from is running in the background (see FLAG_ACTIVITY_REORDER_TO_FRONT)
  • The dialog activity must show a dialog somehow, e.g. by creating a class which extends the Dialog class directly

在启动对话框活动的代码中:

In the code that starts the dialog activity:

Intent intent = new Intent(baseActivity, DialogActivity.class);
// NEW_TASK allows the new dialog activity to be separate from the existing activity.
// REORDER_TO_FRONT causes the dialog activity to be moved to the front,
// if it's already running.
// Without the NEW_TASK flag, the existing "base" activity
// would be moved to the front as well.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(DialogActivity.EXTRA_SOME_PARAM, someParamValue);
// The activity must be started from the application context.
// I'm not sure why exactly.
baseActivity.getApplicationContext().startActivity(intent);

在上面,baseActivity是对应用程序主要活动的引用.

In the above, baseActivity is a reference to the main activity of the application.

将对话框活动的launchMode设置为singleInstance可能会有所帮助,确保对话框活动中从不积累其他活动,但这可能是不必要的. @android:style/Theme.Translucent.NoTitleBar主题允许其下面的活动显示出来.

It may help to give the dialog activity a launchMode of singleInstance, ensuring that it never accumulates other activities in its task, but this may be unnecessary. The @android:style/Theme.Translucent.NoTitleBar theme allows the activity underneath it to show through.

<activity
    android:name=".DialogActivity"
    android:launchMode="singleInstance"
    android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>

对于对话框活动本身,可能有必要调整其窗口以确保它不会填满整个屏幕:

For the dialog activity itself, it may be necessary to adjust its window to ensure that it doesn't fill the whole screen:

 getWindow().setLayout(
    ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.WRAP_CONTENT
);

同样,在对话框活动的布局XML中,可能也有必要:

Likewise, in the dialog activity's layout XML, it may also be necessary:

android:layout_width="fill_parent"
android:layout_height="wrap_content"

对于对话框本身,您可以做很多事情,但是一种解决方案是扩展Dialog类:

For the dialog itself, you can do a lot of things, but one solution is to extend the Dialog class:

class DialogActivity extends Dialog { ... }

要显示活动中的对话框,只需创建Dialog的新实例并调用其show()方法.

To show the dialog from the activity, just create a new instance of the Dialog and call its show() method.

这篇关于在后台显示另一个应用程序上的对话框活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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