没有从Notification PendingIntent创建Android后台堆栈

问题描述 投票:6回答:1

如果在遵循通知时应用程序不在内存中,我就会遇到问题。不会创建backstack。我按照开发人员指南按照步骤操作。请告诉我我错过的位,否则我将必须通过我的HomeActivity路由所有意图,以便按照以下意图手动创建后台。

AndroidManifest.xml中:

<activity
    android:name=".activity.HomeActivity"
    android:clearTaskOnLaunch="true"
    android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    android:icon="@drawable/actionbar_logo"
    android:label="@string/activity_label_home"
    android:launchMode="singleTask"
    android:parentActivityName=".activity.Start"
    android:windowSoftInputMode="stateAlwaysHidden" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".activity.Start" />
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
<activity
    android:name=".activity.ChatActivity"
    android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    android:label="@string/activity_label_in_chat"
    android:parentActivityName=".activity.HomeActivity"
    android:windowSoftInputMode="stateHidden"
    tools:ignore="UnusedAttribute" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".activity.HomeActivity" />

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="vnd.android.cursor.item/vnd.myapp.chat" />
    </intent-filter>
</activity>

建筑通知:

final String chatId = cursor.getString(cursor.getColumnIndexOrThrow(MessageColumns.CHAT));
final Intent chat = new Intent(c, ChatActivity.class);
chat.putExtra(ChatActivity.EXTRA_CHAT_ID, chatId);
PendingIntent intent = TaskStackBuilder.create(c).addNextIntentWithParentStack (chat).getPendingIntent (0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder (c);
final NotificationManagerCompat nm = NotificationManagerCompat.from (c);
builder
    .setSmallIcon (R.drawable.ic_stat_notification)
    .setContentIntent (intent)
    .setGroup (GROUP_KEY_MYAPP)
    .setGroupSummary (true);
    Notification notification = builder.build ();
    nm.notify(NOTIFICATION_ID, notification);

从操作栏处理主页点击:

public void onHomeActionDefault (final Activity baseActivity) {
    Keyboard.close (baseActivity);
    Intent upIntent = NavUtils.getParentActivityIntent (baseActivity);
    if (null != upIntent) {
        if (NavUtils.shouldUpRecreateTask (baseActivity, upIntent)) {
            android.support.v4.app.TaskStackBuilder.create (baseActivity)
                                                   .addNextIntentWithParentStack (upIntent)
                                                   .startActivities ();
        } else {
            NavUtils.navigateUpTo (baseActivity, upIntent);
        }
    } else {
        NavUtils.navigateUpFromSameTask (baseActivity);
    }
}

这里有什么我想念的吗?

android notifications back-stack
1个回答
7
投票

事实证明,我需要为intent添加一个标志并使用应用程序上下文。我不需要在目标类中的java中完全创建backstack,只需要在manifest.xml中。所以我构建了这个小方法来为我正确且一致地构建待定意图。

public static PendingIntent addBackStack(final Context context, final Intent intent) {
   TaskStackBuilder stackBuilder = TaskStackBuilder.create (context.getApplicationContext ());
   stackBuilder.addNextIntentWithParentStack (intent);
   intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
   return stackBuilder.getPendingIntent (0,PendingIntent.FLAG_UPDATE_CURRENT);
}
© www.soinside.com 2019 - 2024. All rights reserved.