Notification 的创建主要涉及到 Notification.Builder 、 Notification 、 NotificationManager 。

  1. Notification.Builer : 使用建造者模式构建 Notification 对象。由于 Notification.Builder 仅支持 Android 4.1及之后的版本,为了解决兼容性问题, Google 在 Android Support v4 中加入了 NotificationCompat.Builder 类。对于某些在 Android 4.1 之后才特性,即使 NotificationCompat.Builder 支持该方法,在之前的版本中也不能运行。点我 查看更多关于 Notification 兼容性问题处理。文中使用的都是 NotificationCompat。
  2. Notification : 通知对应类,保存通知相关的数据。NotificationManager 向系统发送通知时会用到。
  3. NotificationManager : NotificationManager 是通知管理类,它是一个系统服务。调用 NotificationManager 的 notify() 方法可以向系统发送通知。

1.获取 NotificationManager 对象:

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2.Builder

a.普通通知

 Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.lanucher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("普通通知");
selectNotofovatiomLevel(builder);
notificationManager.notify(0, builder.build());

b.折叠式通知

Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("折叠式通知");
selectNotofovatiomLevel(builder);
//用RemoteViews来创建自定义Notification视图
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
Notification notification = builder.build();
//指定展开时的视图
notification.bigContentView = remoteViews;
notificationManager.notify(1, notification);

c.悬挂式通知

Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("悬挂式通知");
selectNotofovatiomLevel(builder);
//设置点击跳转
Intent hangIntent = new Intent();
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
hangIntent.setClass(this, MyNotificationActivity.class);
//如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true); notificationManager.notify(2, builder.build());

3.效果

android-基础编程-Notification的更多相关文章

  1. android: 多线程编程基础

    9.1   服务是什么 服务(Service)是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那 些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖于任何用户界面,即使 ...

  2. Android网络编程基础

    Android网络编程只TCP通信 TCP 服务器端工作的主要步骤如下.步骤1 调用ServerSocket(int port)创建一个ServerSocket,并绑定到指定端口上.步骤2 调用acc ...

  3. Android基础总结(8)——服务

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  4. Qt on Android 核心编程

    Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著   ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...

  5. Android网络编程系列 一 TCP/IP协议族

    在学习和使用Android网路编程时,我们接触的仅仅是上层协议和接口如Apache的httpclient或者Android自带的httpURlconnection等等.对于这些接口的底层实现我们也有必 ...

  6. Android网络编程系列 一 Socket抽象层

     在<Android网络编程>系列文章中,前面已经将Java的通信底层大致的描述了,在我们了解了TCP/IP通信族架构及其原理,接下来我们就开始来了解基于tcp/ip协议层的Socket抽 ...

  7. 【Xamarin开发 Android 系列 4】 Android 基础知识

    原文:[Xamarin开发 Android 系列 4] Android 基础知识 什么是Android? Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Li ...

  8. Android网络编程概述

    Android网络编程概述 首先,应该了解的几个问题: 1)Android平台网络相关API接口 a) java.net.*(标准Java接口) java.net.*提供与联网有关的类,包括流.数据包 ...

  9. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关类 简介

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...

  10. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...

随机推荐

  1. java、asp.net 通用分页码函数

    <script type="text/javascript"> $(document).ready(function(){ ajaxGetPaging(1); }); ...

  2. Docker端口映射

    Docker端口映射是指将容器内应用的服务端口映射到本机宿主机器.当要在宿主机外部访问Docker内部应用时,需要对容器内应用端口进行映射. 一.容器启动时指定端口映射 容器运行时可以通过-p 或 - ...

  3. Jenkins+Gradle+Docker打docker镜像包上传至s3

    gradle打包跟maven打包的环境搭建有相似之处,可参考maven打包https://www.cnblogs.com/chenchen-tester/p/6408815.html 进入Jenkin ...

  4. window下mongodb的安装和环境搭建

    一.下载安装包或者压缩包 1.下载 mongodb官网社区版下载页面 开发者一般使用社区版即可 3.6.3版本仅支持64位版本 2.安装 mongo compass是一个图形界面管理工具,安装过程非常 ...

  5. CSRedisCore 在net core中的使用

    背景:与net core配套的StackExchange.Redis客户端总是间歇性的发生timeout异常. 由complexer单例对象创建的IDatabase对象,在产生Timeout异常后会导 ...

  6. hbase-连接流程

    root和meta表 在版本0.9.8之前,存在root表,之后的版本中去除了root表,meta表主要记录了每个表在region的分布情况. meta结构 从表格中可以看出,rowkey格式:tab ...

  7. jquery.validate 远程验证remote使用详解

    目的: 试了一下远程验证,试了好几次,得出小结,可以记录下,提醒以防绕路. ----------------直接贴图,省事明了---------------- 1.引用js 2.demo的html & ...

  8. iptables命令提取总结,包含扩展模块<取自朱双印博客>

    以下内容只是一些命令相关的,以朱双印博客中的iptables的教程提取出来的.纯粹只是命令的总结,如果需要看理论的知识,建议去看朱老师的博客,目前还没有看到写得比这个好的了. <http://w ...

  9. c#简单的数据库查询与绑定DataGridView。

    1配置文件 (两种写法) <connectionStrings>    <add name="connStr" connectionString="se ...

  10. Linux无法正常连接服务器,无法连接上 127.0.0.1:8989 (127.0.0.1)。 - connect (111: 拒绝连接)

    最近修改了下电脑的hosts文件,电脑就突然不能连接下载更新的服务器了,但是浏览器还能正常上网,这让我很是难受啊!!! 错误现象如下: 错误:1 http://archive.ubuntukylin. ...