Android文档笔记:通知(三) - 启…
来源:互联网 发布:服务器端口开启 编辑:程序博客网 时间:2024/06/05 02:55
-当你从一个通知启动活动时,你必须保留用户所希望的导航体验:
>按回退键应当通过应用正常的工作流将用户带回到Home屏。
>点击“最近使用”则会将刚才启动的活动显示为一个单独的任务。
- 要做到这一点,应当从一个全新的任务来启动活动
-如何配置PendingIntent来得到一个全新的任务?这取决于你的活动的性质:
常规活动:
-这指的是,你启动的活动是应用程序正常工作流的一部分。此时,创建一个PendingIntent来开始新的任务,同时要提供给它一个backstack以重现在应用程序中正常的回退行为。
-如在Gmail应用中,如果通过通知栏打开单封邮件,系统会首先进入Gmail,显示邮件列表再打开对应的邮件。用户回退时也要路过这些打开的活动。
- 这与你当前是否恰好在那个应用之中没有关系。例如,你正在Gmail中书写一封邮件,此事通过通知栏点开一封邮件,所经历的回退顺序仍然是打开的邮件->收件箱->Home屏,而不会回退到之前的书写屏。我认为,原因在于,书写和后来打开邮件这两个操作并不在同一个任务中。
特殊活动
- 此时从通知栏打开活动,用户仅仅看到次活动。
-从某种意义上说,开启这个活动的目的是展示通知中难以显示的信息。
-在这种情况下,通过创建PendingIntent来启动新任务,无须创建一个backstack。原因在于,启动的活动并非应用程序活动流的一部分。点击回退键则将把用户直接带回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创建一个backstack
a. 创建Intent
b. 调用TaskStackBuilder.create()创建栈构建器
c.调用
addParentStack()
将backstack加入到栈构建器。对于每个处于定义好的层级中活动,backstack都持有一个启动它的Intent对象。本方法还添加了从新任务启动这个栈的标记。d.添加从通知启动活动的Intent: 调用addNextIntent(),并以第一步中创建的intent作为参数
e.如果需要,调用TaskStackBuilder.editIntentAt()为栈中的intent对象添加参数。有时通过这个途经来确保用户回退后到达的活动能够显示有意义的数据。
f.调用getPendingIntent()从backstack获得我们想要的PendingIntent。
...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); stackBuilder
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
创建特殊活动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());
- Android文档笔记:通知(三) - 启…
- Android文档笔记:通知(四)&nbsp…
- Android文档笔记:服务(四)- 通…
- android 通知系统
- OC 之 KVO、KVC、通知(笔记)
- android notification通知的…
- android o…
- android …
- mysql 笔记三 (联系正则表达式)
- [unity3d] iTween文档解析(3) (…
- This Android SDK …
- android gallery…
- Android edittext&nbsp…
- 解决 Android tex…
- Android 平台 ROM …
- Sprite Kit学习笔记(三)
- Android基础 : Android Con…
- Android基础 : Android Con…
- Android文档笔记:服务(四)- 通…
- Android文档笔记:通知(一)
- Android文档笔记:通知(二)
- Android进程与线程(二):线程
- Android进程与线程(三):跨进程…
- Android文档笔记:通知(三) - 启…
- Android文档笔记:通知(四)&nbsp…
- Android绑定服务(一):基础知识
- POJ 2411 记录状态的dp
- Android绑定服务(二):创建绑定…
- Android用户界面(一):概览
- springMVC and AJAX with JSON (转载)
- Android用户界面(二):布局
- Android用户界面(三):线性布局…