哪个Android Intent标志使用

bar*_*rry 4 android android-intent android-activity

我有一个接收广播的应用程序AlarmManager.在此之后,它启动一个透明Activity(AlarmAlertDialogActivity)然后显示一个AlertDialog.AlertDialog在呼叫中单击取消结果finish().

由于AlarmAlertDialogActivity它不是从另一个发射,Activity而是一个广播接收器,它是用它发射的

Intent.FLAG_ACTIVITY_NEW_TASK
Run Code Online (Sandbox Code Playgroud)

这意味着Activity将在新任务中启动.

我的问题是,当应用程序在取消之后从最近的历史中重新启动AlertDialog(即通过按住主页按钮并单击应用程序的图标),AlertDialog将重新启动.我希望通过使用finish()/ Intentflags我可以避免这种情况; 我想要发生的是在要启动的父活动Activity之前的最后一次AlertDialog.

Intent.FLAG_ACTIVITY_NO_HISTORY在启动时尝试使用bitmasking 作为附加标志,AlarmAlertDialogActivity但这似乎没有任何区别.

Bitmasking Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS有效,但只能从最近的历史中删除应用程序(顾名思义).这对用户体验不利.

那么,是否有可能获得我正在寻找的UI流程?

更新 - 根据要求提供更多信息:

来自Broadcast接收器的Logcat,AlertDialog活动和我的主要活动:

    05-30 10:36:00.132: D/everyOtherApp(362): Received alarm broadcast at: Wed May 30 10:36:00 GMT+00:00 2012
05-30 10:36:00.262: D/everyOtherApp(362): AlarmAlertDialogActivity.onCreate()
05-30 10:36:00.912: D/everyOtherApp(362): AlarmAlertDialogActivity.onResume()
05-30 10:36:12.461: D/everyOtherApp(362): Cancel pressed

//Cancel exits the activity. I now relaunch the app from recent history:

05-30 10:36:20.233: D/everyOtherApp(362): AlarmAlertDialogActivity.onCreate()
05-30 10:36:21.621: D/everyOtherApp(362): AlarmAlertDialogActivity.onResume()
Run Code Online (Sandbox Code Playgroud)

从BroadcastReceiver启动Activity的代码:

        Intent intent = new Intent(new Intent(applicationContext, AlarmAlertDialogActivity.class));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Constants.SCHEDULED_ALARM_TAG, alarm);
    applicationContext.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

manfest文件中的AlarmAlertDialogActivity:

    <activity
        android:name=".AlarmAlertDialogActivity"
        android:theme="@android:style/Theme.NoDisplay" >
    </activity>
Run Code Online (Sandbox Code Playgroud)

Dav*_*ser 7

我在另一个项目中做了类似的事情.我有一个BroadcastReceiver,它获取有关数据连接和SIM-Profile更改的信息,并显示一个对话框(使用像你这样的活动)警告用户他可能会收取费用.我最终做的是以下内容:

在清单中,在<Activity>AlarmAlertDialogActivity 的标记中,添加以下内容:

android:excludeFromRecents="true"
android:noHistory="true"
android:taskAffinity=""
Run Code Online (Sandbox Code Playgroud)

说明:设置excludeFromRecentsnoHistory"true"确保活动不会显示在最近的应用程序列表中,并且一旦用户离开它,他将无法返回(这可能是你想要的) ).设置taskAffinity为空字符串可确保AlarmAlertDialogActivity将在其自己的任务中运行,即使您的应用程序在显示对话框时正在运行.

只要您有另一个活动作为您的应用程序的主要活动(即:使用action.MAIN和的过滤器category.LAUNCHER),这应该可以解决您的问题.