android 4.0 前后很多api都有了较大的差别,不多说现在学习下notification前后实现的差别,本文参考了

:http://my.oschina.net/ososchina/blog/353692;http://gundumw100.iteye.com/blog/1873318;

http://blog.csdn.net/wukunting/article/details/5661769

先把没有注释的代码贴上,不明白的还可以看下面的带带注释的代码,希望对跟我一样的初学者有所帮助:

代码:

public class MainActivity extends ActionBarActivity {private static final int NOTIFY_ID = 0; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void tell(View v){//  **************android 4.0后*************************************String title="mynotice";//通知栏的标题String store="hahagaga";NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //点击notification需要激活的activity,这里设置自己Intent intent = new Intent(this,MainActivity.class);  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   intent.putExtra("store", store);  PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT  Notification notification = new Notification.Builder(getApplicationContext())           .setContentTitle(title)  .setContentText(store)  .setContentIntent(contentIntent)  .setSmallIcon(R.drawable.hahagaga)   .setAutoCancel(true) .setWhen(System.currentTimeMillis())
//                             .setDefaults(Notification.DEFAULT_ALL)  //所有的设为系统默认,一般和短信通知一样.build();//led灯的显示notification.ledARGB = Color.RED;notification.ledOffMS = 0;notification.ledOnMS = 1;notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;notification.flags |= Notification.FLAG_AUTO_CANCEL;long[] vibrate = new long[] { 1000, 1000, 1000, 1000};notification.vibrate = vibrate;Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.zp1);notification.sound=uri;notificationManager.notify(NOTIFY_ID, notification); //第一个参数是标识通知的唯一码,上面自己定义的变量//  **************android 4.0前*************************************
//      String title="mynotice";//通知栏的标题
//      String store="hahagaga";
//       NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//         Notification notification = new Notification();
//         notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//         notification.flags |= Notification.FLAG_AUTO_CANCEL;
//         notification.defaults = Notification.DEFAULT_ALL;
//         notification.icon = R.drawable.hahagaga;
//         notification.when = System.currentTimeMillis();
//
//         Intent intent = new Intent(this,MainActivity.class);
//         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//         intent.putExtra("store", store);
//         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT
//          //Change the name of the notification here
//         notification.setLatestEventInfo(this, title, store, contentIntent);
//         notificationManager.notify(NOTIFY_ID, notification);  }

  

注释:  

public class MainActivity extends ActionBarActivity {private static final int NOTIFY_ID = 0; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void tell(View v){//  **************android 4.0后*************************************String title="mynotice";//通知栏的标题String store="hahagaga";NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //intent是点击notification后激活的意图Intent intent = new Intent(this,MainActivity.class);
//       注意:如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
//       Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的
//                       所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
//       Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,
//                      若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建
//               Activity。更多请参见 “ (转载)Android下Affinities和Task ”intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   intent.putExtra("store", store);  //contentIntent是修饰intent的
//         Intent :意图,即告诉系统我要干什么,然后系统根据这个Intent做对应的事。如startActivity相当于发送消息,
//                  而Intent是消息的内容。
//         PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast
//         启动某项工作的意图。而某些时候, 我们并不能直接调用startActivity , startServide 或 sendBroadcast ,
//         而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发
//         出一条Activity 的 Intent 。因此如果我们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity,
//         startService 还是 sendBroadcast 来启动Intent 的(当然还有其他的“描述”),因此这里便需要PendingIntent。PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT  Notification notification = new Notification.Builder(getApplicationContext())           .setContentTitle(title)  .setContentText(store)  .setContentIntent(contentIntent)  .setSmallIcon(R.drawable.hahagaga)   .setAutoCancel(true) .setWhen(System.currentTimeMillis())
//                             .setDefaults(Notification.DEFAULT_ALL)  //所有的设为系统默认,一般和短信通知一样,设置默认后面的设置就不起作用了.build();//设置默认   DEFAULT_LIGHTS  默认灯//DEFAULT_SOUND   默认声音//DEFAULT_VIBRATE  默认震动//DEFAULT_ALL 以上默认//led灯的显示notification.ledARGB = Color.RED;notification.ledOffMS = 0;notification.ledOnMS = 1;notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;
//         通常为了简便写成notification.flags |= Notification.FLAG_SHOW_LIGHTS;,这样可以为通知设置多个flagnotification.flags |= Notification.FLAG_AUTO_CANCEL;//通知来时候震动,vibrate是震动的方式,
//      long[] vibrate  :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒 long[] vibrate = new long[] { 1000, 1000, 1000, 1000};notification.vibrate = vibrate;//         设置声音
//         获取工程文件下的资源的uri方式和获取sd卡路径的方式//Uri.parse("/data/data/zp1.mp3");Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.zp1);notification.sound=uri;//启动notificationnotificationManager.notify(NOTIFY_ID, notification); //第一个参数是标识通知的唯一码,上面自己定义的变量//  **************android 4.0前*************************************
//      String title="mynotice";//通知栏的标题
//      String store="hahagaga";
//       NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//         Notification notification = new Notification();
//         notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//         notification.flags |= Notification.FLAG_AUTO_CANCEL;
//         notification.defaults = Notification.DEFAULT_ALL;
//         notification.icon = R.drawable.hahagaga;
//         notification.when = System.currentTimeMillis();
//
//         Intent intent = new Intent(this,MainActivity.class);
//         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//         intent.putExtra("store", store);
//         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT
//          //Change the name of the notification here
//         notification.setLatestEventInfo(this, title, store, contentIntent);
//         notificationManager.notify(NOTIFY_ID, notification);  }}

  

转载于:https://www.cnblogs.com/bokeofzp/p/4744214.html

Notification的学习,4.0前后的差别,和在设置声音的时候获取资源的uri方法相关推荐

  1. 学习记录:关于通过使用express,设置reffer和host获取qq音乐数据

    因为qq音乐歌手信息的获取,设置了reffer(设置了url的来源)和host(决定了虚拟机上的哪个站点), 所以通过使用express配置路由, 通过axios来获取数据, 将数据返回给前端请求的路 ...

  2. Maven 学习 (0) Maven 简介

    2019独角兽企业重金招聘Python工程师标准>>> Maven 学习 (0) Maven 简介 什么是 Maven? Apache Maven 是一个软件项目的管理和理解工具.M ...

  3. 初识图机器学习(part5)--图表示学习2.0

    学习笔记,仅供参考,有错必纠 文章目录 图表示学习2.0 图表示学习2.0 的 分类 基于分解的方法 例子(推荐系统) 基于随机游走的方法 基于深度学习的方法 基于深度学习的图表示一定比基于分解和随机 ...

  4. 初识图机器学习(part4)--图表示学习1.0

    学习笔记,仅供参考,有错必纠 文章目录 图表示学习1.0 图表示学习的含义 图表示学习1.0 的分类 LLE Laplacian Eigenmaps Cauchy Graph Embedding 图表 ...

  5. 资源下载| 深度学习Pytoch1.0如何玩?这一门含900页ppt和代码实例的深度学习课程带你飞

    本文来自专知 近日,在NeurIPS 2018 大会上,Facebook 官方宣布 PyTorch 1.0 正式版发布了.如何用Pytorch1.0搞深度学习?对很多小白学生是个问题.瑞士非盈利研究机 ...

  6. 从零开始学习SFR-- 1.0

    从零开始学习SFR--1.0 镜头质量检测相关基础知识 1.分辨率 1.1光学分辨率和图像分辨率 1.2系统分辨率 1.3相机的像元.像素.相机分辨率 1.4镜头"像素"与镜头分辨 ...

  7. 乐鑫esp8266学习rtos3.0笔记第9篇:整理分享那些我在项目中常用的esp8266 rtos3.0版本的常见驱动,Button按键长短按、PWM平滑调光等。(附带demo)

    本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 1. Esp8266之 搭建开发环境,开始一个"hello ...

  8. Notification通知全文 7.0 and 8.0通知 ,横幅 ,显示,抖动,响铃

    RemoteViews在更改通知(Notification)中经常会用到 RemoteViews所支持的View类型如下图所示(注意不支持下图中View的子类): RemoteViews所支持的Vie ...

  9. 从零开始学习SFR-- 2.0

    从零开始学习SFR-- 2.0 1.SFR基础 2.刃边法(e-SFR) 2.1 SFR算法流程 2.2 PSF.LSF.ESF 总结 前言:因为课题涉及镜头质量检测,而现在镜头检测最普遍的方法便是M ...

最新文章

  1. 支持异步同步的分布式CommandBus MSMQ实现 - 支持Session传递、多实例处理
  2. 【“探探”为例】手把手教你用最少的代码实现各种“机器人”
  3. 操作系统第三章-内存管理
  4. 中国二手房行业发展形势与“十四五”前景规划建议报告2022-2028年
  5. Python单元测试介绍及单元测试理解,单元测试的自动生成(对函数进行测试)
  6. 联想 键盘 fn linux,开发者提交补丁,Linux 5.10 或支持联想 PC 键盘快捷键
  7. 解决pytouch导入模型报错:AttributeError: Can‘t get attribute ‘XXX‘ on <module ‘__main__‘ from XXX>
  8. python冒泡排序时间复杂度_Python算法中的时间复杂度问题
  9. glassfish启动后不能进入部署页面_Flink on Yarn三部曲之二:部署和设置
  10. 【最小生成树】还是畅通工程
  11. ajax 详解(GET,POST方式传输以其封装)
  12. C# 类和结构的成员
  13. 学计算机数理化不好怎么办,数理化不好怎么办?3招教你学好数理化!
  14. 企业级Web报表工具告诉你财务要做哪些数据分析
  15. L4RE学习笔记——服务介绍
  16. 五星级洒店系统需求分析
  17. flutter 单、双排按钮及选择联动ui
  18. ubuntu14.04+caffe2
  19. 云计算学习路线教程大纲课堂笔记:构建企业级WIKI及工单系统
  20. centos7修改主机名的方法

热门文章

  1. windows延缓写入失败相关问题解决办法
  2. 前端学习(3248):react的脚手架文件
  3. [css] 如何解决IE6浮动时产生双倍边距的BUG?
  4. [js] for in 和 for of 的区别?
  5. 工作88:vue实现当前页面刷新
  6. 前端学习(2080):计算属性和methods得对比
  7. 前端学习(1147):ES6学习目标
  8. 前端学习(726):如何交换变量值
  9. spring学习(26):更优雅的依赖注入 在@bean注入参数
  10. 27.用zxing生成二维码