您的位置:首页 > 移动开发 > Android开发

Android文档笔记:通知(三) - 启动活动时保存导航

2014-03-24 16:55 567 查看
原文地址: http://blog.sina.com.cn/s/blog_597a014f0101930t.html

- 当你从一个通知启动活动时,你必须保留用户所希望的导航体验:

> 按回退键应当通过应用正常的工作流将用户带回到Home屏。

> 点击“最近使用”则会将刚才启动的活动显示为一个单独的任务。

- 要做到这一点,应当从一个全新的任务来启动活动

- 如何配置PendingIntent来得到一个全新的任务?这取决于你的活动的性质:

常规活动:

- 这指的是,你启动的活动是应用程序正常工作流的一部分。此时,创建一个PendingIntent来开始新的任务,同时要提供给它一个back stack以重现在应用程序中正常的回退行为。

- 如在Gmail应用中,如果通过通知栏打开单封邮件,系统会首先进入Gmail,显示邮件列表再打开对应的邮件。用户回退时也要路过这些打开的活动。

- 这与你当前是否恰好在那个应用之中没有关系。例如,你正在Gmail中书写一封邮件,此事通过通知栏点开一封邮件,所经历的回退顺序仍然是打开的邮件->收件箱->Home屏,而不会回退到之前的书写屏。我认为,原因在于,书写和后来打开邮件这两个操作并不在同一个任务中。

特殊活动

- 此时从通知栏打开活动,用户仅仅看到次活动。

- 从某种意义上说,开启这个活动的目的是展示通知中难以显示的信息。

- 在这种情况下,通过创建PendingIntent来启动新任务,无须创建一个back stack。原因在于,启动的活动并非应用程序活动流的一部分。点击回退键则将把用户直接带回Home屏。

创建常规活动PendingIntent

- 在manifest中定义你的应用的活动层级:

1. 增加对安卓4.0.3及更早版本的支持,即在元素中制定此活动的父活动:

> android:name="android.support.PARENT_ACTIVITY"

> android:value=""

2. 同时也添加对4.1和以后版本的支持:直接为活动设置android:parentActivityName属性

android:name=".MainActivity"
android:label="@string/app_name" >

android:name="android.intent.action.MAIN" />
android:name="android.intent.category.LAUNCHER" />

android:name=".ResultActivity"
android:parentActivityName=".MainActivity">

android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>


2. 基于启动活动的Intent创建一个back stack

a. 创建Intent

b. 调用TaskStackBuilder.create()创建栈构建器

c. 调用
addParentStack()
将back
stack加入到栈构建器。对于每个处于定义好的层级中活动,back stack都持有一个启动它的Intent对象。本方法还添加了从新任务启动这个栈的标记。

d. 添加从通知启动活动的Intent: 调用addNextIntent(),并以第一步中创建的intent作为参数

e. 如果需要,调用TaskStackBuilder.editIntentAt()为栈中的intent对象添加参数。有时通过这个途经来确保用户回退后到达的活动能够显示有意义的数据。

f. 调用getPendingIntent()从back
stack获得我们想要的PendingIntent。

android:name=".MainActivity"
android:label="@string/app_name" >

android:name="android.intent.action.MAIN" />
android:name="android.intent.category.LAUNCHER" />

android:name=".ResultActivity"
android:parentActivityName=".MainActivity">

android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>


创建特殊活动PendingIntent
- 防止使用应用程序缺省任务:android:taskAffinity=""
- 将新任务排除在最近任务之外,防止用户偶然的切换到它:android:excludeFromRecents="true"

<activity
android:name=".ResultActivity"
...
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
...


// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Creates an Intent for the Activity
Intent notifyIntent =
new Intent(new ComponentName(this, ResultActivity.class));
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
PendingIntent notifyIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent
PendingIntent.FLAG_UPDATE_CURRENT
);

// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyIntent);
// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, builder.build());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: