关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种。今天就稍微记录下几种Notification的用法。3.0以前的通知和3.0以后的通知是有些区别的。话不多说,直接上代码。

  1、新建一个android项目

    我新建项目的 minSdkVersion="11",targetSdkVersion="19"。也就是支持最低版本的3.0的。

  2、习惯性地打开项目清单文件AndroidManifest.xml,添加一个权限:<uses-permission android:name="android.permission.VIBRATE"/> 不添加不行的。

  3、在布局activity_main.xml中添加几个按钮,样子就大概这样,垂直排版的LinearLayout

具体代码

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. android:orientation="vertical"
  10. tools:context=".MainActivity" >
  11.  
  12. <Button
  13. android:id="@+id/btn_01"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:text="3.0以前版本的notification,用新的吧"
  17. android:onClick="click"
  18. />
  19.  
  20. <Button
  21. android:id="@+id/btn_02"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:text="大视图文本通知"
  25. android:onClick="click"
  26. />
  27.  
  28. <Button
  29. android:id="@+id/btn_03"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="大视图图片通知"
  33. android:onClick="click"
  34. />
  35.  
  36. <Button
  37. android:id="@+id/btn_04"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:text="进度条通知"
  41. android:onClick="click"
  42. />
  43.  
  44. </LinearLayout>

  4、MainActivity中的代码:

  1. package com.xin.day_12_notificationdemo1;
  2.  
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5.  
  6. import android.app.Activity;
  7. import android.app.Notification;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.content.Intent;
  11. import android.graphics.BitmapFactory;
  12. import android.os.Bundle;
  13. import android.support.v4.app.NotificationCompat;
  14. import android.support.v4.app.NotificationCompat.BigPictureStyle;
  15. import android.support.v4.app.NotificationCompat.BigTextStyle;
  16. import android.support.v4.app.NotificationCompat.Builder;
  17. import android.util.Log;
  18. import android.view.View;
  19.  
  20. public class MainActivity extends Activity {
  21.  
  22. //通知的唯一标识,在一个应用程序中不同的通知要区别开来
  23. private static final int NO1 = 0x1001;
  24. private static final int NO2 = 0x1002;
  25. private static final int NO3 = 0x1003;
  26. private static final int NO4 = 0x1004;
  27. //进度条要用
  28. private int progress = 1;
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. }
  35.  
  36. //click方法,和xml文件中的各个按钮的onClick属性的值要一致
  37. public void click(View view) {
  38. //创建NotificationManager
  39. final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  40. //用switch语句控制四个控件
  41. switch (view.getId()) {
  42. case R.id.btn_01: {
  43. Notification notification = new Notification();
  44. notification.icon = R.drawable.ic_launcher;
  45. notification.tickerText = "有消息了。。。";
  46. Intent intent = new Intent(this, MainActivity.class);
  47. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
  48. intent, PendingIntent.FLAG_UPDATE_CURRENT);
  49. notification.setLatestEventInfo(this, "3.0以前的通知", "试试而已", pendingIntent);
  50. notification.when = System.currentTimeMillis();
  51. notification.defaults = Notification.DEFAULT_ALL;
  52. notification.flags = Notification.FLAG_AUTO_CANCEL;
  53. notification.number = 1;
  54. notification.vibrate = new long[]{0, 4000};
  55. manager.notify(NO1, notification);
  56. }
  57. break;
  58. case R.id.btn_02:{
  59. //大视图文本通知
  60. //创建消息构造器,在扩展包
  61. NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  62. //设置当有消息是的提示,图标和提示文字
  63. builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了");
  64. //需要样式
  65. BigTextStyle style = new BigTextStyle();
  66. style.setBigContentTitle("上课通知");//通知的标题
  67. style.bigText("今天下午要在综B303上jsp");//通知的文本内容
  68. //大视图文本具体内容
  69. style.setSummaryText("这是正常的课程安排,请各位同学按时上课");
  70. builder.setStyle(style);
  71. //显示消息到达的时间,这里设置当前时间
  72. builder.setWhen(System.currentTimeMillis());
  73. //获取一个通知对象
  74. Notification notification = builder.build();
  75. notification.flags = Notification.FLAG_AUTO_CANCEL;
  76. //发送(显示)通知
  77. //notify()第一个参数id An identifier for this notification unique within your application
  78. //get?意思说,这个通知在你的应用程序中唯一的标识符
  79. manager.notify(NO2, notification);
  80. }
  81. break;
  82.  
  83. case R.id.btn_03:{
  84. //大视图图片通知
  85. NotificationCompat.Builder builderPic = new Builder(this);
  86. builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪体育提醒");
  87. //进行设置
  88. BigPictureStyle pictureStyle = new BigPictureStyle();
  89. pictureStyle.setBigContentTitle("新浪体育 快船VS骑士 ");
  90. pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game));
  91. pictureStyle.setSummaryText(" 快船VS骑士 天王山之战!!!");//不要在意文字
  92. //设置样式
  93. builderPic.setStyle(pictureStyle);
  94. //设置显示的时间
  95. builderPic.setWhen(System.currentTimeMillis());
  96. Notification notification = pictureStyle.build();
  97. notification.flags = Notification.FLAG_AUTO_CANCEL;
  98. //
  99. manager.notify(NO3, notification);
  100. }
  101. break;
  102.  
  103. case R.id.btn_04:{
  104. //进度条通知
  105. final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this);
  106. builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("进度条通知");
  107. builderProgress.setProgress(100, progress, false);
  108. final Notification notification = builderProgress.build();
  109. //发送一个通知
  110. manager.notify(NO4, notification);
  111. //创建一个计时器
  112. Timer timer = new Timer();
  113. timer.schedule(new TimerTask(){
  114.  
  115. @Override
  116. public void run() {
  117. Log.i("progress",progress+"");
  118. while(progress <= 100){
  119. progress ++;
  120. try {
  121. Thread.sleep(300);
  122. } catch (InterruptedException e) {
  123. // TODO Auto-generated catch block
  124. e.printStackTrace();
  125. }
  126. //更新进度条
  127. builderProgress.setProgress(100, progress, false);
  128. //再次通知
  129. manager.notify(NO4, builderProgress.build());
  130. }
  131. //计时器退出
  132. this.cancel();
  133. //进度条退出
  134. manager.cancel(NO4);
  135. return;//结束方法
  136. }
  137.  
  138. }, 0);
  139. }
  140. break;
  141.  
  142. default:
  143. break;
  144. }
  145. }
  146.  
  147. }

  5、运行:我的虚拟机版本是4.0的,按住通知左(右)滑动就可以让通知小时了。效果如下:

android的几种“通知”方式简单实现(Notification&NotificationManager)的更多相关文章

  1. Android中几种定位 方式

    介绍的几种定位方式 http://www.cnblogs.com/cuihongyu3503319/p/3863867.html 百度地图api: http://lbsyun.baidu.com/in ...

  2. Android学习笔记(36):Android的两种事件处理方式

    Android提供了两种事件处理的方式:基于回调的事件处理 和 基于监听的事件处理. 我们来说的easy理解一点: (1)基于回调的事件处理就是继承GUI组件,并重写该组件的事件处理方法.除了一些特定 ...

  3. android service两种启动方式

    android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...

  4. Android的四种储存方式(SQLite、FileSystem、SDCardSystem、SharedPreferences)

    主要记录一下安卓中几种常用的存储方式的用法. 一.SQLite 1.创建SQLiteOpenHelper对象(当然SQLiteOpenHelper是抽象类,不能直接创建): 2.通过上面创建的对象调用 ...

  5. android开发两种退出程序方式(killProcess,System.exit)

    KillProcess: 在android中我们如果想要程序的进程结束可以这样写: android.os.Process.killProcess(android.os.Process.myPid()) ...

  6. Android 文件下载三种基本方式

    一.自己封装URLConnection 连接请求类 public void downloadFile1() { try{ //下载路径,如果路径无效了,可换成你的下载路径 String url = & ...

  7. android:Activity四种启动模式简单介绍

    Activity启动模式 能够依据实际的需求为Activity设置相应的启动模式,从而能够避免创建大量反复的Activity等问题 Activity有四种载入模式 1.standard(默认启动模式, ...

  8. Android进程通信之一:两种序列化方式

    2月下旬辞职了,去海南度假到现在,领略了一把三亚风情也算任性和 然而这样任性带来的后果就是..不行了我必须吐槽一句.. 没毕业的找工作就这么难嘛!投了57家一家面试机会都没有,好歹给个面试机会啊!!本 ...

  9. Android平台中实现对XML的三种解析方式

    本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...

随机推荐

  1. 6) mvn archetype:create-from-project

    cd %old%mvn archetype:create-from-projectcd %old%/target/generated-sources/archetype mvn installcd % ...

  2. Codeforces758A Holiday Of Equality 2017-01-20 10:08 48人阅读 评论(0) 收藏

    A. Holiday Of Equality time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. hdu 4995 离线处理+模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=4995 给定一维坐标下的n个点,以及每个点的权值,有m次查询,每次将查询的x点上的权值修改为离x最近的k个点权值的 ...

  4. UFOV页面 使用canvas

    canvas画八边形:cxt.beginPath();cxt.beginPath(); canvas内线条的粗细:cxt.lineWidth = '2'; 鼠标消失: css: html, body ...

  5. 取得 APP 自己的版本号 (跨 4 个平台)

    http://www.cnblogs.com/onechen/p/3627942.html XE7 源码下载:[原创]取得APP自己的版本号(狠跨4个平台)XE7.zip XE6 源码下载:[原创]取 ...

  6. CentOS搭建NFS服务

    系统结构 ----------------------------------------------------------------------------------------------- ...

  7. 全自动baidu云盘下载脚本

    20141231<吃元宵>孔云龙_6平米.MP3 20141231<家庭论>李云杰_6平米.MP3 20141231<劫皇杠>李昊洋_6平米.MP3 2014123 ...

  8. 在微信开发中如果WeixinJSBridge.call('closeWindow');关闭窗口无效!

    原因是,成功后页面跳转到普通页面.必须在前面加上 parent.WeixinJSBridge.call('closeWindow'); 这样才行.如果是使用了iframe页面,这样也可以关闭网页,回到 ...

  9. SSRS (一)创建基础报表

    ReportService创建基础报表 1.数据库SQL Server2012选择SQL Server Data Tools 2.创建商业智能(BI)项目 选择报表服务器项目 ReportServic ...

  10. 企业项目开发--本地缓存guava cache(1)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.在实际项目开发中,会使用到很多缓存技术,而且数据库的设计一般也会依赖于有缓存的情况下设计. 常用的缓存分 ...