通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。
 
通知的基本用法
 
既可以在活动里创建,也可以在广播接收器里创建,相比于广播接收器和服务,在活动里创建通知的场景还是比较少的,因为一般只有当程序进入到后台的时候我们才需要使用通知。论是在哪里创建通知,整体的步骤都是相同的。
  • 首先需要一个 NotificationManager 来对通知进行管理,可以调用 Context 的getSystemService()方法获取到。getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务,这里我们传入 Context.NOTIFICATION_SERVICE 即可。因此,获取NotificationManager 的实例就可以写成:
                NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  • 接下来需要创建一个 Notification 对象,这个对象用于存储通知所需的各种信息,我们可以使用它的有参构造函数来进行创建。Notification 的有参构造函数接收三个参数,第一个参数用于指定通知的图标,比如项目的 res/drawable 目录下有一张 icon.png 图片,那么这里就可以传入 R.drawable.icon。第二个参数用于指定通知的 ticker 内容,当通知刚被创建的时候,它会在系统的状态栏一闪而过,属于一种瞬时的提示信息。第三个参数用于指定通知被创建的时间,以毫秒为单位,当下拉系统状态栏时,这里指定的时间会显示在相应的通知上。因此,创建一个 Notification 对象就可以写成:

Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis());

  • 创建好了 Notification 对象后,我们还需要对通知的布局进行设定,这里只需要调用Notification 的 setLatestEventInfo()方法就可以给通知设置一个标准的布局。这个方法接收四个参数,第一个参数是 Context,这个没什么好解释的。第二个参数用于指定通知的标题内容,下拉系统状态栏就可以看到这部分内容。第三个参数用于指定通知的正文内容,同样下拉系统状态栏就可以看到这部分内容。第四个参数我们暂时还用不到,可以先传入 null。因此,对通知的布局进行设定就可以写成:

notification.setLatestEventInfo(context, "This is content title", "This iscontent text", null);

  • 以上工作都完成之后,只需要调用 NotificationManager 的 notify()方法就可以让通知显示出来了notify()方法接收两个参数,第一个参数是 id,要保证为每个通知所指定的 id 都是不同的。第二个参数则是 Notification 对象,这里直接将我们刚刚创建好的 Notification 对象传入即可。因此,显示一个通知就可以写成:
                manager.notify(1, notification);
 
  1. import android.app.Activity;
  2. import android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.RemoteViews;
  11. publicclassMyNotificationActivityextendsActivity{
  12. privateButton btn_notify1;
  13. privateNotificationManager nManager;
  14. privateNotification notification ;
  15. @Override
  16. protectedvoid onCreate(Bundle savedInstanceState){
  17. // TODO Auto-generated method stub
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.layout_notification);
  20. //得到notification管理器
  21. nManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  22. btn_notify1 =(Button)findViewById(R.id.btn_notify1);
  23. btn_notify1.setOnClickListener(newOnClickListener(){
  24. @Override
  25. publicvoid onClick(View v){
  26. // TODO Auto-generated method stub
  27. PendingIntent piIntent =PendingIntent.getActivity(MyNotificationActivity.this,1,newIntent(MyNotificationActivity.this,FormActivity.class),1);
  28. /*Notification notification = new Notification(R.drawable.p2409, "You have a message", System.currentTimeMillis());
  29. notification.setLatestEventInfo(MyNotificationActivity.this, "Racoon", "Love U", piIntent);
  30. */
  31. //创建notification实例
  32. notification =newNotification.Builder(MyNotificationActivity.this)
  33. .setContentText("Love U")
  34. .setContentTitle("little Racoon")
  35. .setTicker("You have a new message")
  36. .setSmallIcon(R.drawable.peasy)//状态栏的图标
  37. .setContentIntent(piIntent)
  38. .getNotification();
  39. notification.contentView =newRemoteViews(getPackageName(), R.layout.layout_customnotification);
  40. //把notification发布到状态栏
  41. nManager.notify(1, notification);
  42. }
  43. });
  44. }
  45. @Override
  46. protectedvoid onStop(){
  47. // TODO Auto-generated method stub
  48. nManager.cancelAll();
  49. super.onStop();
  50. }
  51. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/btn_notify1"
  8. style="?android:attr/buttonStyleSmall"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="Common notify"/>
  12. </LinearLayout>
现在就可以来运行一下程序了,点击 Common notify 按钮,就会看到有一条通知在系统状态栏显示出来,如图所示。
 
 
下拉系统状态栏可以看到该通知的详细信息,如图所示
 
 
如果你使用过 Android 手机,此时应该会下意识地认为这条通知是可以点击的。但是当你去点击它的时候,你会发现没有任何效果。不对啊,好像每条通知点击之后都应该会有反应的呀?其实要想实现通知的点击效果,我们还需要在代码中进行相应的设置,这就涉及到了一个新的概念,PendingIntent。
 
PendingIntent 从名字上看起来就和 Intent 有些类似,它们之间也确实存在着不少共同点。比如它们都可以去指明某一个“意图”,都可以用于启动活动、启动服务以及发送广播等。不同的是,Intent 更加倾向于去立即执行某个动作,而 PendingIntent 更加倾向于在某个合适的时机去执行某个动作。所以,也可以把 PendingIntent 简单地理解为延迟执行的 Intent。PendingIntent并不是Intent!
 
 
PendingIntent 的用法同样很简单,它主要提供了几个静态方法用于获取 PendingIntent 的实例,可以根据需求来选择是使用 getActivity()方法、getBroadcast()方法、还是 getService()方法。这几个方法所接收的参数都是相同的,第一个参数依旧是 Context,不用多做解释。第二个参数一般用不到,通常都是传入 0 即可。第三个参数是一个 Intent 对象,我们可以通过这个对象构建出 PendingIntent 的“意图”。第四个参数用于确定 PendingIntent 的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT 和 FLAG_UPDATE_CURRENT 这四种值可选,每种值的含义你可以查看文档。
 
怎么系统状态上的通知图标还没有消失呢?是这样的,如果我们没有在代码中对该通知进行取消,它就会一直显示在系统的状态栏上显示。解决的方法也很简单,调用NotificationManager 的 cancel()方法就可以取消通知了。
 
 

初识Notification的更多相关文章

  1. 初识zookeeper(1)之zookeeper的安装及配置

    初识zookeeper(一)之zookeeper的安装及配置 1.简要介绍 zookeeper是一个分布式的应用程序协调服务,是Hadoop和Hbase的重要组件,是一个树型的目录服务,支持变更推送. ...

  2. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  3. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  4. android Notification介绍

    如果要添加一个Notification,可以按照以下几个步骤 1:获取NotificationManager: NotificationManager m_NotificationManager=(N ...

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

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

  6. Missing Push Notification Entitlement 问题

    最近打包上传是遇到一个问题: 描述: Missing Push Notification Entitlement - Your app includes an API for Apple's Push ...

  7. 笔记:Memory Notification: Library Cache Object loaded into SGA

    笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...

  8. ABP源码分析二十四:Notification

    NotificationDefinition: 用于封装Notification Definnition 的信息.注意和Notification 的区别,如果把Notification看成是具体的消息 ...

  9. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

随机推荐

  1. 10、scala模式匹配

    一.模式匹配1 1.介绍 模式匹配是Scala中非常有特色,非常强大的一种功能.模式匹配,其实类似于Java中的swich case语法,即对一个值进行条件判断,然后针对不同的条件, 进行不同的处理. ...

  2. Cannot uninstall 'enum34'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    更新tensorflow时遇到报错 Found existing installation: enum34 1.0.4Cannot uninstall 'enum34'. It is a distut ...

  3. 8.Struts2-057漏洞复现

    漏洞信息: 定义XML配置时如果namespace值未设置且上层动作配置(Action Configuration)中未设置或用通配符namespace时可能会导致远程代码执行. url标签未设置va ...

  4. Citrix 未注册解决办法

    Citrix 经常出现未注册的问题 是因为DNS的解析 问题 ping DDC 的全名你会发现ping 不通 解决方案如下 首先 在 192.168.1.145(图站)上饭解析一下DDC(控制中心19 ...

  5. CSS类名命名规则

    CSS样式命名 说明 网页公共命名 #wrapper 页面外围控制整体布局宽度 #container或#content 容器,用于最外层 #layout 布局 #head, #header 页头部分 ...

  6. Spark 中的 RPC 的几个类

    Spark 中 RPC 部分的涉及了几个类,有点晕,在此记录一下 1. RpcEndpoint: RPC的一个端点.给定了相应消息的触发函数.保证  `onStart`, `receive` and ...

  7. RabbitMQ简介和使用

    一.RabbitMQ简介 1.什么是RabbitMQ AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设 ...

  8. Jenkins+maven+gitlab+shell实现项目自动化部署

    确认jdk , maven,git这些已经在服务器上搭建成功,gitlab使用的是公司服务也没有进行搭建 下面是jenkins的两种搭建方式 1.      第一种比较简单下载对应jenkins.wa ...

  9. Github网站css加载不出来的处理方法(转,亲测有效)

    转载链接:https://blog.csdn.net/qq_36589706/article/details/80573008因为工作需求,自己会经常使用到github,但是突然有一天打开github ...

  10. POJ 3537 Crosses and Crosses(sg博弈)

    题目:在1*n 的棋盘里面,A和B都在里面画叉 , 如果谁可以画了一个叉后,可以连成3个叉,那谁胜利 : 分析: 首先考虑如果我在玩游戏,我最希望对手可以画出-x-x or  -xx-   ,  这种 ...