Android Notification 使用方法详解
Android Notification 使用方法详解
用TaskStackBuilder来获取PendingIntent处理点击跳转到别的Activity,首先是用一般的PendingIntent来进行跳转。
mBuilder = new NotificationCompat.Builder(this).setContent(view)
.setSmallIcon(R.drawable.icon).setTicker("新资讯")
.setWhen(System.currentTimeMillis())
.setOngoing(false)
.setAutoCancel(true);
Intent intent = new Intent(this, NotificationShow.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
直接用PendingIntent来跳转到NotificationShow,在运行效果上来看,首先发送了一条Notification到通知栏上,然后这时,我退出程序,即MainActivity已经不存在了,回到home主菜单,看到Notification仍然存在,当然,我们还没有点击或者cancel它,现在去点击Notification,跳转到NotificationShow界面,然后我们按下Back键,发现直接回到主界面了。现在大多数android应用都是在通知栏中如果有Notification通知的话,点击它,然后会直接跳转到对应的应用程序的某个界面,这时如果回退,即按下Back键,会返回到该应用程序的主界面,而不是系统的主界面。所以用上面这种PendingIntent的做法达不到目的。这里我们使用TaskStackBuilder来做。
mBuilder = new NotificationCompat.Builder(this).setContent(view)
.setSmallIcon(R.drawable.icon).setTicker("新资讯")
.setWhen(System.currentTimeMillis())
.setOngoing(false)
.setAutoCancel(true);
Intent intent = new Intent(this, NotificationShow.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationShow.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
// intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
显示用TaskStackBuilder.create(this)一个stackBuilder实例,接下来addParentStack();关于这个方法,我们查一下官方API文档:Add the activity parent chain as specified by the parentActivityName attribute of the activity (or activity-alias) element in the application's manifest to the task stack builder. 这句话是说添加一个activity,与这个activity的manifest文件中的parentActivityName的属性相关联。
那么我们就在manifest文件中添加这个属性
<activity android:name="com.shulf.notificationtest.NotificationShow" android:parentActivityName=".MainActivity" > </activity>
让它的parentActivity为MainActivity,也就是说在NotificationShow这个界面点击回退时,会跳转到MainActivity这个界面,而不是像上面一样直接回到了程序的主菜单。运行一下,最后效果确实是这样。
以上实用Android Notification的实例详解,如有疑问请留言或者到本站社区交流讨论,本站关于Android开发的文章还有很多,希望大家搜出查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
- Android编程使用Service实现Notification定时发送功能示例
- Android 通知使用权(NotificationListenerService)的使用
- Android Notification的多种用法总结
- Android Notification使用方法详解
- Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏
- android使用NotificationListenerService监听通知栏消息
- Android实现Service下载文件,Notification显示下载进度的示例
- Android Notification使用方法总结
相关文章
Android RecyclerView网格布局(支持多种分割线)详解(2)
这篇文章主要为大家详细介绍了Android RecyclerView网格布局,支持多种分割线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-02-02
Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果
这篇文章主要介绍了Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2020-01-01
Android编程实现获取当前系统语言及地区并更改语言的方法
这篇文章主要介绍了Android编程实现获取当前系统语言及地区并更改语言的方法,涉及Android针对系统语言及地区的获取与设置相关操作技巧,需要的朋友可以参考下2017-10-10
Android通过Handler与AsyncTask两种方式动态更新ListView(附源码)
这篇文章主要介绍了Android通过Handler与AsyncTask两种方式动态更新ListView的方法,结合实例形式分析了ListView动态更新的常用技巧,并附上完整实例源码供读者下载,需要的朋友可以参考下2015-12-12
Android的RV列表刷新详解Payload与Diff方式异同
这篇文章主要为大家介绍了Android的RV列表刷新详解Payload与Diff方式异同,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-10-10
Android Studio 设置代码提示和代码自动补全快捷键方式
这篇文章主要介绍了Android Studio 设置代码提示和代码自动补全快捷键方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-03-03


最新评论