初识Notification
- 首先需要一个 NotificationManager 来对通知进行管理,可以调用 Context 的getSystemService()方法获取到。getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务,这里我们传入 Context.NOTIFICATION_SERVICE 即可。因此,获取NotificationManager 的实例就可以写成:
- 接下来需要创建一个 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 对象传入即可。因此,显示一个通知就可以写成:
import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RemoteViews;publicclassMyNotificationActivityextendsActivity{privateButton btn_notify1;privateNotificationManager nManager;privateNotification notification ;@Overrideprotectedvoid onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.layout_notification);//得到notification管理器nManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);btn_notify1 =(Button)findViewById(R.id.btn_notify1);btn_notify1.setOnClickListener(newOnClickListener(){@Overridepublicvoid onClick(View v){// TODO Auto-generated method stubPendingIntent piIntent =PendingIntent.getActivity(MyNotificationActivity.this,1,newIntent(MyNotificationActivity.this,FormActivity.class),1);/*Notification notification = new Notification(R.drawable.p2409, "You have a message", System.currentTimeMillis());notification.setLatestEventInfo(MyNotificationActivity.this, "Racoon", "Love U", piIntent);*///创建notification实例notification =newNotification.Builder(MyNotificationActivity.this).setContentText("Love U").setContentTitle("little Racoon").setTicker("You have a new message").setSmallIcon(R.drawable.peasy)//状态栏的图标.setContentIntent(piIntent).getNotification();notification.contentView =newRemoteViews(getPackageName(), R.layout.layout_customnotification);//把notification发布到状态栏nManager.notify(1, notification);}});}@Overrideprotectedvoid onStop(){// TODO Auto-generated method stubnManager.cancelAll();super.onStop();}}
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/btn_notify1"style="?android:attr/buttonStyleSmall"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Common notify"/>- </LinearLayout>




初识Notification的更多相关文章
- 初识zookeeper(1)之zookeeper的安装及配置
初识zookeeper(一)之zookeeper的安装及配置 1.简要介绍 zookeeper是一个分布式的应用程序协调服务,是Hadoop和Hbase的重要组件,是一个树型的目录服务,支持变更推送. ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- android Notification介绍
如果要添加一个Notification,可以按照以下几个步骤 1:获取NotificationManager: NotificationManager m_NotificationManager=(N ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- Missing Push Notification Entitlement 问题
最近打包上传是遇到一个问题: 描述: Missing Push Notification Entitlement - Your app includes an API for Apple's Push ...
- 笔记:Memory Notification: Library Cache Object loaded into SGA
笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...
- ABP源码分析二十四:Notification
NotificationDefinition: 用于封装Notification Definnition 的信息.注意和Notification 的区别,如果把Notification看成是具体的消息 ...
- Android动画效果之初识Property Animation(属性动画)
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
随机推荐
- 10、scala模式匹配
一.模式匹配1 1.介绍 模式匹配是Scala中非常有特色,非常强大的一种功能.模式匹配,其实类似于Java中的swich case语法,即对一个值进行条件判断,然后针对不同的条件, 进行不同的处理. ...
- 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 ...
- 8.Struts2-057漏洞复现
漏洞信息: 定义XML配置时如果namespace值未设置且上层动作配置(Action Configuration)中未设置或用通配符namespace时可能会导致远程代码执行. url标签未设置va ...
- Citrix 未注册解决办法
Citrix 经常出现未注册的问题 是因为DNS的解析 问题 ping DDC 的全名你会发现ping 不通 解决方案如下 首先 在 192.168.1.145(图站)上饭解析一下DDC(控制中心19 ...
- CSS类名命名规则
CSS样式命名 说明 网页公共命名 #wrapper 页面外围控制整体布局宽度 #container或#content 容器,用于最外层 #layout 布局 #head, #header 页头部分 ...
- Spark 中的 RPC 的几个类
Spark 中 RPC 部分的涉及了几个类,有点晕,在此记录一下 1. RpcEndpoint: RPC的一个端点.给定了相应消息的触发函数.保证 `onStart`, `receive` and ...
- RabbitMQ简介和使用
一.RabbitMQ简介 1.什么是RabbitMQ AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设 ...
- Jenkins+maven+gitlab+shell实现项目自动化部署
确认jdk , maven,git这些已经在服务器上搭建成功,gitlab使用的是公司服务也没有进行搭建 下面是jenkins的两种搭建方式 1. 第一种比较简单下载对应jenkins.wa ...
- Github网站css加载不出来的处理方法(转,亲测有效)
转载链接:https://blog.csdn.net/qq_36589706/article/details/80573008因为工作需求,自己会经常使用到github,但是突然有一天打开github ...
- POJ 3537 Crosses and Crosses(sg博弈)
题目:在1*n 的棋盘里面,A和B都在里面画叉 , 如果谁可以画了一个叉后,可以连成3个叉,那谁胜利 : 分析: 首先考虑如果我在玩游戏,我最希望对手可以画出-x-x or -xx- , 这种 ...