从 Service 启动同一 Activity 的多个实例
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就是这么简单。有标志
FLAG_ACTIVITY_MULTIPLE_TASK
根据文档:正是我所需要的。感谢并抱歉回答我的问题。这不是一种习惯。 :)
It was so simple. There is the flag
FLAG_ACTIVITY_MULTIPLE_TASK
which according to the documentation :Was exactly what I need. Thanks and sorry for answering on my question. It is not a habit. :)
服务将使用标志
FLAG_ACTIVITY_NEW_TASK
来启动活动,但在这里您可以尝试如下操作:设置您想要多个实例的活动的
处理程序实例
,在服务中。当您需要新的 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:Set the
instance of the handler
of the activity of which you want multiple instances, in the service.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.我猜您的应用程序在后台运行,即使应用程序目前不在前台,也会显示弹出窗口,对吧?
否则我会使用普通的弹出窗口(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