android - 使用哪个 Android Intent 标志

标签 android android-intent android-activity

我有一个从 AlarmManager 接收广播的应用程序。在此基础上,它启动一个透明的 Activity (AlarmAlertDialogActivity),然后显示一个 AlertDialog。单击 AlertDialog 上的取消会调用 finish()

由于 AlarmAlertDialogActivity 不是从另一个 Activity 启动的,而是从广播接收器启动的,因此它是通过

Intent.FLAG_ACTIVITY_NEW_TASK

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

我的问题是,当应用程序在取消 AlertDialog(即按住主页按钮并单击应用程序的图标)后从最近的历史记录中重新启动时,AlertDialog 会重新启动。我曾希望通过使用 finish()/Intent 标志可以避免这种情况;我希望发生的是 AlertDialog 的父 Activity 启动之前的最后一个 Activity

我曾尝试在启动 AlarmAlertDialogActivity 时将位掩码 Intent.FLAG_ACTIVITY_NO_HISTORY 作为附加标志,但这似乎没有任何区别。

位掩码 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 有效,但只能通过从最近的历史记录中删除该应用程序(顾名思义)。这不利于用户体验。

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

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

来自广播接收器的 Logcat、AlertDialog Activity 和我的主要 Activity :

    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()

从 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);

list 文件中的 AlarmAlertDialogActivity:

    <activity
        android:name=".AlarmAlertDialogActivity"
        android:theme="@android:style/Theme.NoDisplay" >
    </activity>

最佳答案

我在另一个项目中做了类似的事情。我有一个 BroadcastReceiver,它获取有关数据连接和 SIM 配置文件更改的信息,并显示一个对话框(使用像您这样的 Activity )警告用户他可能会产生费用。我最终做了以下事情:

在 list 中,在 <Activity> 中为您的 AlarmAlertDialogActivity 标记添加以下内容:

android:excludeFromRecents="true"
android:noHistory="true"
android:taskAffinity=""

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

只要您有另一个 Activity 作为应用程序的主要 Activity (即:使用 action.MAINcategory.LAUNCHER 的 Intent 过滤器),这应该可以解决您的问题。

关于android - 使用哪个 Android Intent 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10790202/

相关文章:

android - ACTION_MEDIA_BUTTON 在 BroadcastReceiver 中不起作用

android - 当taskAffinity Activity 正在调用finish()但它没有关闭最近的应用程序时

Android 注释 - 未找到默认 Activity

android - 将当前 Intent 作为额外的内容传递给另一个 Activity

android - 我什么时候需要基础 Activity 和基础 fragment ?

android - 用户登录后,我不希望再次显示登录 Activity ,除非用户注销

android - glReadPixels 使用太慢

Android map 当前位置和目的地的缩放级别

android - 从Gallery RuntimeException中选择图像

android - Android上从相机 Intent 获取图像