Notification可以让我们在获得消息的时候,在状态栏,锁屏界面来显示相应的信息,很难想象如果没有Notification,那我们的qq和微信以及其他应用没法主动通知我们,我们就需要时时的看手机来检查是否有新的信息和提醒着实让人烦心,也体现出Notification重要性。这里会介绍三种Notification,分别是普通的Notification,折叠式Notification和悬挂式Notification。

1. 普通Notification

首先创建Builder 对象,用PendingIntent 控制跳转,这里跳转到网页

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, , mIntent, );

有了builder 我们就可以给Notification添加各种属性:

builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.lanucher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("普通通知");

最后是创建NotificationManager调用notify方法:

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(, builder.build());

来看看效果:

2. 折叠式Notification

折叠式Notification是一种自定义视图的Notification,用来显示长文本和一些自定义的布局的场景。它有两种状态,一种是普通状态下的视图(如果不是自定义的话和上面普通通知的视图样式一样),一种是展开状态下的视图。和普通Notification不同的是,我们需要自定义的视图,而这个视图显示的进程和我们创建视图的进程不再一个进程,所以我们需要使用RemoteViews,首先要使用RemoteViews来创建我们的自定义视图:

//用RemoteViews来创建自定义Notification视图
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);

视图的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/fold"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/fold"
/>
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="150dp"
android:text="展开后的自定义视图"
android:textColor="@color/colorPrimaryDark"/>
</LinearLayout>

我们需要把自定义的视图赋值给Notification的视图,下面代码是把自定义视图赋值给Notification展开时的视图

//指定展开时的视图
notification.bigContentView = remoteViews;

当然我们也可以把自定义视图赋值给Notification普通状态时的视图

//指定普通状态时的视图
notification.contentView = remoteViews;

其他的代码和普通Notification没什么区别,折叠式Notification完整代码:

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, , mIntent, );
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("折叠式通知");
//用RemoteViews来创建自定义Notification视图
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
Notification notification = builder.build();
//指定展开时的视图
notification.bigContentView = remoteViews;
notificationManager.notify(, notification);

如果不是自定义普通状态视图的话,折叠式Notification普通状态下和普通Notification没什么区别

我们接着往下拉,使折叠式Notification完全展开就会出现我们自定义的视图

3. 悬挂式Notification

悬挂式Notification是android5.0新增加的方式,和前两种显示方式不同的是,前两种需要下拉通知栏才能看到通知,而 悬挂式Notification不需要下拉通知栏就直接显示出来悬挂在屏幕上方并且焦点不变仍在用户操作的界面因此不会打断用户的操作,过几秒就会自动消失。
和前两种Notification不同的是,他需要调用setFullScreenIntent来将Notification变为悬挂式Notification

//如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, , hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);

实现悬挂式Notification完整代码:

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, , mIntent, );
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("悬挂式通知");
//设置点击跳转
Intent hangIntent = new Intent();
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
hangIntent.setClass(this, MyNotificationActivity.class);
//如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, , hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);
notificationManager.notify(, builder.build());

来看看效果

4. Notification的显示等级

android5.0加入了一种新的模式Notification的显示等级,共有三种:

  • VISIBILITY_PUBLIC 任何情况都会显示通知
  • VISIBILITY_PRIVATE 只有在没有锁屏时会显示通知
  • VISIBILITY_SECRET 在pin、password等安全锁和没有锁屏的情况下才能够显示

设置非常简单只要调用setVisibility方法就可以了

builder.setVisibility(Notification.VISIBILITY_PUBLIC);

我在这里写了个方法来设置Notification等级,用radioGroup来演示Notification的各个显示等级,详情请参照源码。

private void selectNotofovatiomLevel(Notification.Builder builder) {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.rb_public:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setContentText("public");
break;
case R.id.rb_private:
builder.setVisibility(Notification.VISIBILITY_PRIVATE);
builder.setContentText("private");
break;
case R.id.rb_secret:
builder.setVisibility(Notification.VISIBILITY_SECRET);
builder.setContentText("secret");
break;
default:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setContentText("public");
break; }
}

源码下载

Android5.x Notification应用解析的更多相关文章

  1. i.mx6 Android5.1.1 build解析

    参考资料:理解 Android Build 系统 把总结放前面: 1. 常用编译命令 make clean 执行清理,等同于:rm -rf out/. make sdk 编译出 Android 的 S ...

  2. Android Bug 记录

    1.Unable to resolve target 'android-5' 无法解析目标 ' 安卓系统-5'      Unable to resolve target 'Google Inc.:G ...

  3. Activity组件安全(下)

    什么是Activity劫持 简单的说就是APP正常的Activity界面被恶意攻击者替换上仿冒的恶意Activity界面进行攻击和非法用途.界面劫持攻击通常难被识别出来,其造成的后果不仅会给用户带来严 ...

  4. Android Design Support Library(三)用CoordinatorLayout实现Toolbar隐藏和折叠

    此文的代码在Android Design Support Library(一)用TabLayout实现类似网易选项卡动态滑动效果代码的基础上进行修改,如果你没有看过本系列的第一篇文章最好先看一看.Co ...

  5. Android源码博客目录

    每次都找不到,干脆每个部分都开个目录,方便找 0. 杂项 一些Android的博客,没事翻翻 1. 构建相关 linux和Android的Makefile和android.mk android 目录下 ...

  6. Android Design Support Library(一)用TabLayout实现类似网易选项卡动态滑动效果

    这里我们用TabLayout来实现这一效果.TabLayout是Android Design Support Library库中的控件.Google在2015的IO大会上,给我们带来了更加详细的Mat ...

  7. i.mx6 Android5.1.1 初始化流程之init.rc解析(未完成)

    接上一篇:i.mx6 Android5.1.1 初始化流程之init进程 参考资料:http://blog.csdn.net/mr_raptor/article/category/799879 这个博 ...

  8. PureMVC(JS版)源码解析(二):Notification类

    上篇博客,我们已经就PureMVC的设计模式进行的分析,这篇博文主要分析Notification(消息)类的实现. 通过Notification的构造函数可以看出,PureMVC中的Notificat ...

  9. 【直接拿来用のandroid公共代码模块解析与分享】の Notification和NotificationManager

    本文源代码托管在https://github.com/ASCE1885/asce-common,欢迎fork Android项目做得多了.会发现原来非常多基础的东西都是能够复用,这个系列介绍一些自己项 ...

随机推荐

  1. 通过XMLHttpRequest和jQuery两种方式实现ajax

    一.XMLHttpRequest实现获取数据 不使用jQuery实现页面不刷新获取内容的方式,我们这里采用XMLHttpRequest原生代码实现:js代码如下: //1.获取a节点,并为其添加Onc ...

  2. Linux的mv 命令

    mv 命令的10个例子 1.移动文件 移动文件时需要注意的是文件的源地址和目标地址必须不同.这里有个例子,想要将file_1.txt文件从当前目录移动到其它目录,以/home/pungki/为例,语法 ...

  3. makemigrations migrate

    教程 如何重置迁移 (图片:https://www.pexels.com/photo/sky-flying-animals-birds-1209/) Django迁移系统的开发和优化使其能够进行大量迁 ...

  4. Microsoft Azure存储架构设计

    SQL Azure简介 SQL Azure是Azure存储平台的逻辑数据库,物理数据库仍然是SQL Server.一个物理的SQL Server被分成多个逻辑分片(partition),每一个分片成为 ...

  5. Evenbus简单用法

    Evenbus是一个开源插件,可以帮我们在app里面进行数据传递,传递的对象为Object,就是说可以传输任何对象,但是一般为了拓展性和维护性,我们都用来传输Bean类型. 这个插件最重要的是注册和反 ...

  6. 全网最详细的CentOS7里安装MySQL时出现No package mysql-server available错误的解决办法(图文详解)

    不多说,直接上干货! 直接yum install mysql的话会报错,原因在于yum安装库里没有直接可以用的安装包,此时需要用到MariaDB了,MariaDB是MySQL社区开发的分支,也是一个增 ...

  7. 【JAVA】抽象类,抽象方法

    抽象类不能被实例化,有两个特点: 必须继承才有它的用途: 不能描述对象: 抽象方法: 具体实现由子类决定,最终子类必须实现: 没有方法体: 说明: 抽象类不一定包含抽象方法,抽象方法一定是抽象类.

  8. windows平台,实现录音功能详解

    音频处理分为播放和录音两类.对这些处理,微软提供了一些列函数,称之为Waveform Functions.这篇文章讨论录音功能.会对微软提供的函数做简单说明,并对这些函数封装成c++类,再进一步封装成 ...

  9. 分布式锁的两种实现方式(基于redis和基于zookeeper)

    先来说说什么是分布式锁,简单来说,分布式锁就是在分布式并发场景中,能够实现多节点的代码同步的一种机制.从实现角度来看,主要有两种方式:基于redis的方式和基于zookeeper的方式,下面分别简单介 ...

  10. RocketMQ多Master多Slave模式部署

    每个 Master 配置一个 Slave,有多对Master-Slave,HA采用同步双写方式,主备都写成功,向应用返回成功. 优点:数据与服务都无单点,Master宕机情况下,消息无延迟,服务可用性 ...