启动待处理的意图并返回已启动它的活动

时间:2014-04-23 13:02:14

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

我正在创建一个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);
        }

1 个答案:

答案 0 :(得分:2)

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

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

由于PopupsingleInstance活动,当它启动Gmail活动时,该活动将在新任务中启动(因为singleInstance阻止启动任务中的任何其他活动开始singleInstance活动。这可能是为什么从Gmail按回来不会返回到您的应用的部分原因。

正如我之前所说,我担心你的架构存在缺陷,你应该再考虑一下你的需求。