如果已经运行,则阻止启动主要活动 [英] Preventing main activity from being launched if running already
本文介绍了如果已经运行,则阻止启动主要活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个通过intent-filter
动作启动的应用程序.问题在于,每次事件/操作发生时,Android都会显示一个对话框,要求启动该应用程序,即使该应用程序已经启动.
I have an application that is launched via an intent-filter
action. The problem is that every time the event/ action occurs, Android displays a dialog asking to launch the app even if it is already started.
我希望行为如下:
- 如果应用未打开,则用户要求启动该应用.
- 如果应用程序在前台运行,则不会显示对话框.
有没有办法实现这两个目标?我的目标是Android 4.0.
Is there a way to achieve both of these goals? I am targeting Android 4.0.
编辑
这是我的意图过滤器:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
android:resource="@xml/device_filter" />
推荐答案
您需要将Activity
的launchMode
设置为singleTask
.您可以通过在Android清单中添加activity属性来实现此目的:
You need to set the Activity
's launchMode
to singleTask
. You can do this by adding the activity attribute in your Android Manifest:
android:launchMode="singleTask"
有关详细信息,请参见文档.
See the documentation for more details.
这篇关于如果已经运行,则阻止启动主要活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文