【问题标题】:Launch a pending intent and return to activity that has launch it启动待处理的意图并返回已启动它的活动
【发布时间】:2014-04-23 13:02:14
【问题描述】:

我正在创建一个应用程序 android,但我遇到了这种情况的问题.. 我显示一个弹出窗口,这是一个具有半透明背景的活动,它有一个允许在弹出窗口之间滑动的 viewpager。 现在,当我单击此弹出窗口时,我想启动一个 pendingIntent(不是我创建的,而是由外部应用程序创建的......例如由 gMail 交付的 pendingIntent)..

似乎一切正常,但现在问题来了! 如果通过单击弹出窗口我启动了一个外部活动,例如 gMail,当我退出最后一个活动时,我需要返回到包含其他弹出窗口的上一个活动,这有时会发生,但并不总是!这对我来说至关重要,因为我已经在清单文件中为带有标签 android:excludeFromRecents="true" 的弹出窗口设置了我的活动,因此如果我无法返回此活动,我将无法使用其他待处理的弹出窗口。

显然android:excludeFromRecents="true" 的使用对我来说也很重要。

我该如何解决这个问题?

这是我如何从弹出窗口中启动 pendingIntent 的示例:

setOnClickListener(new View.OnViewClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            if(blablabla){
                try{
                    pi.send(context,0,intent);
                }
                catch(Exception e){}
            }
        }
    });

谢谢大家!!!

编辑

我的清单:

<application
    android:allowBackup="true"
    android:icon="..."
    android:label="..."
    android:theme="..." >
    <activity
        android:name="...ActivityApp"
        android:label="..."
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name="...NotificationListener"
        android:label="..."
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>
    <!-- the activity below contains all the popups   -->
    <activity
        android:name="...Popup"
        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent">

    </activity>


</application>

最后是我的侦听器中启动包含弹出窗口的活动的代码:

@Override
public void onNotificationPosted(StatusBarNotification sbn){

    if(sbn.getPackageName().equalsIgnoreCase("com.google.android.gm")){  // GMAIL
            Log.d("GMAIL","ok");
            ...
            Intent gmailIntent = new Intent(getBaseContext(), Popup.class);
            gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ...
            getBaseContext().startActivity(gmailIntent);
        }

【问题讨论】:

  • 我不太明白你的问题。在我看来,您的架构有缺陷。如果您启动一个外部应用程序,用户可以通过按下 HOME 按钮或通过拉下通知栏并转到另一个应用程序来离开该应用程序。无法保证它会“返回”到您的应用程序中。如果您故意阻止您的应用程序出现在最近的任务列表中,那么就没有办法恢复它。也许你应该在这里解释一下你真正想要做什么。
  • 我要做的是创建一个应用程序,该应用程序本质上是一个正在侦听新通知的服务,当它到达时,我拦截这个并显示一个弹出窗口,其中包含通知等。现在,如果我们认为有两个通知到达,我会显示两个弹出窗口。如果我单击一个弹出窗口,我将启动其通知的 pendingIntent 所描述的活动。此时我应该按返回按钮返回我的活动并单击另一个弹出窗口,但是当我单击返回按钮时,我会返回主页而没有剩余的弹出窗口。
  • 另一个重要信息。我确定我的活动没有被破坏,而是处于 onPause 状态!我已经用 Logcat 进行了检查。
  • 但是您不能假设用户会通过“退出”您启动的应用程序来返回您的活动!他可能无法通过按 BACK 来做到这一点。 Android 应用程序并非都这样工作。我仍然认为你的架构有缺陷。无论如何,发布您的清单,也许我可以看到您的应用程序没有重新出现的原因。还要发布您的服务用于启动“弹出”活动的代码。
  • 感谢大卫所有...代码在第一篇文章中(我已经编辑过)!我希望你能帮助我!

标签: android android-intent android-activity android-pendingintent


【解决方案1】:

您正在为您的Popup 活动使用launchMode="singleInstance"。这意味着只会创建此活动的一个实例。当有 2 个通知时,您将调用 startActivity() 2 次,但第二次它不会创建 Popup 的另一个实例,它只会将现有的 Popup 实例带到前台并调用 onNewIntent() on那个实例。

此外,您需要在 Popup 的清单定义中指定 taskAffinity="",因为目前 Popup 活动与您的主要活动具有相同的 taskAffinity,如果您的主要活动是,这可能会导致问题已经运行。如果您确实需要singleInstance 活动,那么您必须确保它不会与您的应用程序中的任何其他活动共享taskAffinity

由于PopupsingleInstance 活动,当它启动Gmail 活动时,该活动将在一个新任务中启动(因为singleInstance 阻止在singleInstance 活动开始的任务中启动任何其他活动) .这可能是从 Gmail 中按“返回”后无法返回您的应用的部分原因。

正如我之前所说,我担心您的架构存在缺陷,您应该重新考虑您的需求。

【讨论】:

  • 感谢您的回答!在继续实施之前,我会更多地考虑我的架构。谢谢大家
猜你喜欢
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
相关资源
最近更新 更多