android 同时发送几条通知

=======

下面是转载的文章。

 同时发送几条通知把ID添加,接收的时候找到这个id就可以出来多条了。

还是不太明白或者编码实现不了的可以加我QQ。

博客很少上了。

========

注意通知中PendingIntent.getActivity(Context context,
int requestCode,
Intent intent, int flags)这几个参数:

context The Context in which this PendingIntent should start the activity.
requestCode    Private request code for the sender (currently not used).
intents Array of Intents of the activities to be launched.
flags May be FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT,
or any of the flags as supported by Intent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.

google API中介绍requestCode中说明是当前未使用,一般都会赋值为0,但是当你发送多个通知,且每个通知都包含Extras时,这个就有用了。这个值可以用来标识不同通知中的Intent,主要是结合后面的flags来使用,比如,发送两个通知,id分别是1和2,当第二次发送1、2的通知时,需要更新前一次通知中的intent内容,如果不用requestCode来标识一下,则默认用最后一次发的通知覆盖前几次的通知intent。

正确是使用方法是:PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);  requestCode来标识不同通知,flags中的PendingIntent.FLAG_UPDATE_CURRENT用来使用后面通知更新前面通知,使用这个flag要注意,intent一定不能为null,否则会报空指针错误。

另外,当我们把Activity 启动模式设置为 singleTask 之后 当我们下次 再去 用Intent 启动 这个 Activity 的时候 就不会去调用 onCreate方法 也不能在onRestart()方法中取,而是去调用onNewIntent()方法
然后把Intent中的数据传给它;

  • <activityandroid:name="TestActivity"
  • android:launchMode="singleTask"/>      示例:

    @Override

      protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);



    int hasMsgNotifyFlag = 0;

    if(intent!=null){

    Bundle bundle = intent.getExtras();

    if(bundle!=null){

    hasMsgNotifyFlag = bundle.getInt("id");

    }

    Log.i( "main tab msg=>", hasMsgNotifyFlag + "");

    }

    }

    /** 

       * 添加一个notification 

       */  

      public void addNotification(Context context, int id, boolean flag){  

          NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  

          Notification notification = new Notification(R.drawable.icon, "hello,here", System.currentTimeMillis());  

          notification.flags = Notification.FLAG_AUTO_CANCEL; 

          Intent intent = new Intent(context, TestActivity.class);  

          Bundle bundle = new Bundle();

          bundle.putInt("id", id);

          intent.putExtras(bundle);

          PendingIntent contentIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);  

          notification.setLatestEventInfo(context, "有新消息", id + "", contentIntent);  

          nm.notify(id, notification);  

      }

  • android 同时发送几条通知的更多相关文章

    1. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

      示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

    2. Android 自学之进度条ProgressBar

      进度条(ProgressBar)也是UI界面中的一种非常使用的组件,通常用于向用户显示某个耗时完成的百分比.因此进度条可以动态的显示进度,因此避免长时间地执行某个耗时操作时,让用户感觉程序失去了响应, ...

    3. Android之发送短信和接收验证码

        最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录    前提---权限     <uses-permission android:name="andro ...

    4. 【转】24. android dialog ——ProgressDialog 进度条对话框详解

      原文网址:http://blog.csdn.net/jamesliulyc/article/details/6375598 首先在onCreateDialog方法里创建一个ProgressDialog ...

    5. Android开发的16条小经验总结

      Android开发的16条小经验总结,希望对各位搞Android开发的朋友有所帮助. 1. TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以 ...

    6. android:Notification实现状态栏的通知

      在使用手机时,当有未接来电或者新短消息时,手机会给出响应的提示信息,这些提示信息一般会显示到手机屏幕的状态栏上. Android也提供了用于处理这些信息的类,它们是Notification和Notif ...

    7. Android如何使用Notification进行通知

      有两张图片素材会放在末尾 activity代码,和XML布局 package com.example.myapplication; import androidx.appcompat.app.AppC ...

    8. PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制

      2013年10月06日最新整理. PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制 微信公共平台消息主动推送接口一直是腾讯的私用接口,相信很多朋友都非常想要用到这个功能. 通过学习 ...

    9. Android View 之进度条+拖动条+星级评论条....

      PS:将来的你会感谢现在奋斗的自己.... 学习内容: 1.进度条 2.拖动条 3.星级评论条 1.进度条...       进图条这东西想必大家是很熟悉的...为了使用户不会觉得应用程序死掉了,因此 ...

    随机推荐

    1. English - therefore,so,hence,then,accordingly,thus用法解析

      这几个词的区别大致可从以下几方面去看: 1.therefore adv.因此, 所以=for that reason=consequently常用于连接两个并列分句,其前加“and”或分号“:”.He ...

    2. .wsdl文件生成.cs文件

      1.打开VS文件命令行工具(一般在安装文件的Common7\Tools\Shortcuts下面例如[E:\VS2013安装程序\Common7\Tools\Shortcuts\VS2013 x64 本 ...

    3. R与数据分析旧笔记(三)不知道取什么题目

      连线图 > a=c(2,3,4,5,6) > b=c(4,7,8,9,12) > plot(a,b,type="l") 多条曲线效果 plot(rain$Toky ...

    4. IOS 特定于设备的开发:Core Motion基础

      Core Motion框架集中了运动数据处理.该框架是在IOS 4 SDK中引入的,用于取代accelerometer加速计访问.它提供了对3个关键的机载传感器的集中式监测.这些传感器有陀螺仪.磁力计 ...

    5. The Highest Mark(01背包)

      The Highest Mark Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

    6. MySQL具体解释(19)----------海量数据分页查询优化

      分页的详细解说解说请看http://blog.csdn.net/u011225629/article/details/46775947 查看代码打印1 SELECT * FROM table ORDE ...

    7. Android基础之在Eclipes中关联SDK源码和查看SDK源码

      在进行Android应用开发的时候,我们有时候需要查看某个类或接口的源码从而了解如何去使用一个类或者实现一个接口,查看源码有助于我们的学习某个封装的类的底层是如何实现的,这样可以帮助我们掌握类或者接口 ...

    8. HDU1046:Gridland

      Problem Description For years, computer scientists have been trying to find efficient solutions to d ...

    9. linux 使用ssh到远端并且使用while的坑

      如果要使用ssh批量登录到其它系统上操作时,我们会采用循环的方式去处理,那么这里存在一个巨大坑,你必须要小心了. 现在是想用一个脚本获取远程服务器端/root下面的文件: #!/bin/bash ca ...

    10. 揭秘Amazon反应速度超快的下拉菜单

      揭秘Amazon反应速度超快的下拉菜单 如果你以前觉得Amazon这家公司不太在用户体验上下功夫,这篇文章可能会改变你的看法. Amazon主页的左上角有一个商品分类浏览的下拉菜单.当鼠标从菜单中的选 ...