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

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

问题描述

假设您有一个应用程序 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).

但是当我这样做时会发生什么情况是对话框被插入在原始应用程序 A 活动和返回堆栈上的应用程序 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)
  • 出于同样的原因,对话框 Activity 不能填满屏幕(参见 Window.setLayout)
  • 对话活动必须是独立于基础活动的任务的一部分,这样当它显示在其他应用程序上方时,它不会也将基础活动拉到其他应用程序上方(请参阅FLAG_ACTIVITY_NEW_TASK)
  • 对话 Activity 必须放在最前面,以便在启动它的 Activity 在后台运行时仍会显示它(参见 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.

给对话活动一个singleInstancelaunchMode可能会有所帮助,确保它永远不会在其任务中积累其他活动,但这可能是不必要的.@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 { ... }

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

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

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

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