从 Service 启动同一 Activity 的多个实例

发布于 2024-12-07 19:57:35 字数 840 浏览 0 评论 0原文

我想从 Service 启动同一 Activity 类的多个实例。我这样做的原因是因为我有一个每天运行“扫描”的服务,如果它发现任何故障,它应该为每个故障显示一个弹出窗口。

我正在启动的Activity更像是一个Dialog,有一个Dialog主题来显示有关故障的信息。

Manfiest:

<activity
   android:name=".ui.dialogs.MalfunctionActivity"
   android:theme="@style/MyDialog"
   android:launchMode="standard">

意图从Service启动活动:

    Intent displayMalf=new Intent(this, MalfunctionActivity.class);
    displayMalf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(displayMalf);

问题:Service启动Activity我需要FLAG_ACTIVITY_NEW_TASK 以某种方式从清单中取消了 launchMode="standard",即使我尝试启动多个不同的实例,也只给我一个 Activity 。 无论如何我可以实现这一目标吗?

I want to start multiple instance of the same Activity class from a Service. The reason I'm doing this, is because I have a Service that runs a "scan" daily, and if it finds any malfunctions it should display a popup for each malfunction.

The Activity that I'm starting is more like a Dialog, has a Dialog theme to display info about the malfunction.

Manfiest:

<activity
   android:name=".ui.dialogs.MalfunctionActivity"
   android:theme="@style/MyDialog"
   android:launchMode="standard">

Intent to start the activity from Service:

    Intent displayMalf=new Intent(this, MalfunctionActivity.class);
    displayMalf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(displayMalf);

PROBLEM: to start the Activity from a Service I need the FLAG_ACTIVITY_NEW_TASK which somehow cancels the launchMode="standard" from the manifest, and gives me just one Activity even if I try to start multiple diffrent instances.
Is there anyway in which I can achieve this?

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

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

发布评论

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

评论(3

指尖微凉心微凉 2024-12-14 19:57:35

就是这么简单。有标志 FLAG_ACTIVITY_MULTIPLE_TASK根据文档:

与 FLAG_ACTIVITY_NEW_TASK 结合使用可禁用将现有任务带到前台的行为。设置后,总是会启动一个新任务来托管该 Intent 的 Activity,无论是否已经有一个现有任务正在运行相同的任务。

正是我所需要的。感谢并抱歉回答我的问题。这不是一种习惯。 :)

It was so simple. There is the flag FLAG_ACTIVITY_MULTIPLE_TASK which according to the documentation :

Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.

Was exactly what I need. Thanks and sorry for answering on my question. It is not a habit. :)

烏雲後面有陽光 2024-12-14 19:57:35

服务将使用标志FLAG_ACTIVITY_NEW_TASK来启动活动,但在这里您可以尝试如下操作:

  1. 设置您想要多个实例的活动的处理程序实例 ,在服务中。

  2. 当您需要新的 Activity 实例时,请使用 handler.sendMessage(msg) 并在您的 Activity 中收到此消息后,再次启动此 Activity。

Service will take the flag FLAG_ACTIVITY_NEW_TASK to start the activity but here you can try like this:

  1. Set the instance of the handler of the activity of which you want multiple instances, in the service.

  2. When you want the new instance of the activity use handler.sendMessage(msg) and on receiving this msg in your activity, start this activity again.

西瓜 2024-12-14 19:57:35

我猜您的应用程序在后台运行,即使应用程序目前不在前台,也会显示弹出窗口,对吧?

否则我会使用普通的弹出窗口(AlertViews)而不是一直启动新的活动。

如果应用程序在后台运行,您可以通过第一个弹出窗口告诉用户您的应用程序发现了一个或多个故障,并且他应该激活该应用程序以获取更多详细信息

I guess your app works in the background and will display the popups even if the app is not in the foreground at the moment, right?

Otherwise I would use normal popup's (AlertViews) instead of starting new activities all the time.

If the app works in the background, you could tell the user with the first popup that your app has found one or more malfunctions and that he should activate the app for more details

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