启动Activity时保留导航

编写:fastcome1985 - 原文:http://developer.android.com/training/notify-user/navigation.html

部分设计一个notification的目的是为了保持用户的导航体验。为了详细讨论这个课题,请看 Notifications API引导,分为下列两种主要情况:

  1. * 常规的activity
  2. 你启动的是你application工作流中的一部分[Activity]($ux-notify-user-developer.android.com-reference-android-app-Activity.html)。
  3. * 特定的activity
  4. 用户只能从notification中启动,才能看到这个[Activity](http://developer.android.com/intl/zh-cn/reference/android/app/Activity.html),在某种意义上,这个[Activity](http://developer.android.com/intl/zh-cn/reference/android/app/Activity.html)是notification的扩展,额外展示了一些notification本身难以展示的信息。

设置一个常规的Activity PendingIntent

设置一个直接启动的入口Activity的PendingIntent,遵循以下步骤:

1 在manifest中定义你application的Activity层次,最终的manifest文件应该像这个:

  1. <activity
  2. android:name=".MainActivity"
  3. android:label="@string/app_name" >
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>
  9. <activity
  10. android:name=".ResultActivity"
  11. android:parentActivityName=".MainActivity">
  12. <meta-data
  13. android:name="android.support.PARENT_ACTIVITY"
  14. android:value=".MainActivity"/>
  15. </activity>

2 在基于启动ActivityIntent中创建一个返回栈,比如:

  1. int id = 1;
  2. ...
  3. Intent resultIntent = new Intent(this, ResultActivity.class);
  4. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  5. // Adds the back stack
  6. stackBuilder.addParentStack(ResultActivity.class);
  7. // Adds the Intent to the top of the stack
  8. stackBuilder.addNextIntent(resultIntent);
  9. // Gets a PendingIntent containing the entire back stack
  10. PendingIntent resultPendingIntent =
  11. stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  12. ...
  13. NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  14. builder.setContentIntent(resultPendingIntent);
  15. NotificationManager mNotificationManager =
  16. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  17. mNotificationManager.notify(id, builder.build());

设置一个特定的Activity PendingIntent

一个特定的Activity不需要一个返回栈,所以你不需要在manifest中定义Activity的层次,以及你不需要调用 addParentStack())方法去构建一个返回栈。作为代替,你需要用manifest设置Activity任务选项,以及调用 getActivity())创建PendingIntent

  1. manifest中,在Activity 标签中增加下列属性: android:name=”activityclass” activity的完整的类名。 android:taskAffinity=”” 结合你在代码里设置的FLAG_ACTIVITY_NEW_TASK标识, 确保这个Activity不会进入application的默认任务。任何与 application的默认任务有密切关系的任务都不会受到影响。 android:excludeFromRecents=”true” 将新任务从最近列表中排除,目的是为了防止用户不小心返回到它。

  2. 建立以及发布notification: a.创建一个启动ActivityIntent. b.通过调用setFlags())方法并设置标识FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASK,来设置Activity在一个新的,空的任务中启动。 c.在Intent中设置其他你需要的选项。 d.通过调用 getActivity()方法从Intent中创建一个 PendingIntent,你可以把这个PendingIntent 当做 setContentIntent()的参数来使用。 下面的代码片段演示了这个过程:

  1. // Instantiate a Builder object.
  2. NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  3. // Creates an Intent for the Activity
  4. Intent notifyIntent =
  5. new Intent(new ComponentName(this, ResultActivity.class));
  6. // Sets the Activity to start in a new, empty task
  7. notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
  8. Intent.FLAG_ACTIVITY_CLEAR_TASK);
  9. // Creates the PendingIntent
  10. PendingIntent notifyIntent =
  11. PendingIntent.getActivity(
  12. this,
  13. 0,
  14. notifyIntent,
  15. PendingIntent.FLAG_UPDATE_CURRENT
  16. );
  17. // Puts the PendingIntent into the notification builder
  18. builder.setContentIntent(notifyIntent);
  19. // Notifications are issued by sending them to the
  20. // NotificationManager system service.
  21. NotificationManager mNotificationManager =
  22. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  23. // Builds an anonymous Notification object from the builder, and
  24. // passes it to the NotificationManager
  25. mNotificationManager.notify(id, builder.build());