Android Notification 使用方法详解

作者:lqh 时间:2021-07-14 14:42:01 

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开发的文章还有很多,希望大家搜出查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:http://blog.csdn.net/man_embedded/article/details/52957981

标签:Android,Notification
0
投稿

猜你喜欢

  • Android自定义View仿探探卡片滑动效果

    2023-03-18 14:54:16
  • Android编程基础之简单Button事件响应综合提示控件Toast应用示例

    2023-06-30 16:27:34
  • Hutool开发MapUtil工具类使用示例

    2022-10-21 22:49:27
  • Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    2023-09-10 07:45:34
  • C#画笔Pen绘制自定义线的帽子

    2022-01-09 20:33:55
  • Java输出链表倒数第k个节点

    2023-03-22 01:22:34
  • Java CharacterEncodingFilter案例详解

    2022-03-21 18:22:32
  • 分享java中设置代理的两种方式

    2023-10-28 10:48:52
  • c# 中文转拼音without CJK

    2023-06-08 23:59:31
  • Java String 拼接字符串原理详解

    2023-05-14 10:10:33
  • IDEA 2020 本土化,真的是全中文了(真香)

    2023-11-25 08:02:58
  • 关于C#数强转会不会抛出异常详解

    2021-11-09 05:44:48
  • Android内部存储改变读取权限的方法

    2021-09-05 13:04:40
  • Android自定义控件之继承ViewGroup创建新容器

    2023-06-15 08:50:11
  • 详解Java实现设计模式之责任链模式

    2023-11-08 10:32:07
  • Jenkins使用Gradle编译Android项目详解

    2021-12-30 22:26:30
  • android使用OkHttp实现下载的进度监听和断点续传

    2022-03-23 12:37:07
  • Java集合框架之Stack Queue Deque使用详解刨析

    2022-06-11 06:10:19
  • SpringBoot之Json的序列化和反序列化问题

    2021-11-12 07:17:29
  • Jar包冲突问题原理及解决方案

    2023-03-05 09:40:02
  • asp之家 软件编程 m.aspxhome.com