上一篇讲了如何创建并显示一个notification,这一篇就总结下点击notification后,程序应该如何响应。
一般来讲,点击一个notification后,都会打开一个Activity做为对点击事件的响应,这个Activity是之前在PendingIntent中设置好的。
经常玩Android手机的应该都有印象,在日历应用中,你新建一个提醒,当提醒通知收到后,你点击通知,会进入提醒的内容页面,如果这个时候按back键,会直接退出应用。
但是在Gmail的应用中,如果有一封新邮件到来,那么点击通知后,会进入到邮件的内容页面,等你看完邮件,点击back键,会退到邮件列表页面,再按back键,才会退出应用。
我们总结一下两种情况,假设我们的应用有两个Activity(ParentActivity、SubActivity),notification中设置打开的Activity为SubActivity。
那么第一种情况就是:
点击Notification ——>进入SubActivity ——> back键 ——> 退出应用
第二种情况:
点击Notification ——>进入SubActivity ——> back键 ——> 退到ParentActivity ——>back键 ——>退出应用
第一种情况比较简单,只需要在PendingIntent中指定Activity,不需要其他设置,Android默认的就这样。
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
但是在创建PendingIntent的时候需要注意参数PendingIntent.FLAG_CANCEL_CURRENT
这个标志位用来指示:如果当前的Activity和PendingIntent中设置的intent一样,那么久先取消当前的Activity,用PendingIntent中指定的Activity取代之。
另外,需要在Manifest中对指定的Activity设置属性
<activity android:name=".SubActivityl"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
第二种情况稍微复杂点,因为如果只打开一个SubActivity,程序并没办法知道他的上一级Activity是谁,所以需要在点击Notification时打开一组Activity,但是我们并不需要一个个去调用startActivity方法,PendingIntent提供了个静态方法getActivities,里面可以设置一个Intent数组,用来指定一系列的Activity。
所以我们首先写一个函数创建一个Activity数组:
Intent[] makeIntentStack(Context context) {
Intent[] intents = new Intent[2];
intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, com.example.notificationtest.MainActivity.class));
intents[1] = new Intent(context, com.example.notificationtest.SubActivity.class);
return intents;
}
其中需要注意的是Intent.makeRestartActivityTask方法,这个方法用来创建activity栈的根activity
接下来,创建并显示Notification:
void showNotification(Intent intent) {
Notification notification = new Notification(
R.drawable.status_icon,
"Hello World ticker text",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivities(
this,
0,
makeIntentStack(this),
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(
this,
"Title",
"Hey, shall we have a dinner tonight",
contentIntent);
notification.flags |= Notification.DEFAULT_ALL;
mNM.notify(1, notification);
}
分享到:
相关推荐
android notification完全解析Demo
简单介绍Android Notification用法 详细介绍NotificationManageService和StatusBarManageService与App层交互过程
安卓Android源码——notification1.rar
主要介绍了android 通知Notification详解及实例代码的相关资料,需要的朋友可以参考下
安卓Android源码——notification.rar
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
安卓Android源码——实现Notification的通知栏常驻.zip
Broadcast Receiver Android 广播事件处理 Broadcast Receiver,讲述了广播事件处理机制、Notification、NotificationManager和AlarmManager的使用 第9章 Android中的数据存取 Android中的数据存取,讲述了Android...
安卓Android源码——实现Notification的通知栏常驻.rar
android 利用notification 通知栏 实现动态下载.
Android Notification使用详解 Notification 核心代码(链式调用):适用于Android 4.0以上(不兼容低版本) Notification noti = new Notification.Builder(this) .setContentTitle(标题名称) .setContentText...
Notification显示音乐下载的界面
类型:NotificationDemo 功能:从网上下载apk存储到手机指定目录,并在通知栏里显示其下载进度,如果下载错误,或者不想要该apk可以删除 涉及知识: 文件流、网络下载链接协议、读写权限、Handler、Notification、...
Android NOtification 使用Android NOtification 使用
android notification 通知 源码 例子
Google在Android 5.0 上又进一步改进了通知栏,优化了Notification。当长按Notification的时候,会显示消息来源。Notification会有一个从白色到灰色的动画切换效果,最终显示发出这个Notification的调用者。同时,在...
这个项目整合了Android平台所有Notification的使用方法,有些使用场景不生效,不知道是否是我使用方法有问题。
android notification Service 文件下载
Android Notification 常见样式代码及发送demo,其中包含源码及一个app-release.apk,可直接发送固定样式,也可自定义样式。可以作为通知测试的小工具,也可作为参考demo。