最近一直在研究 android ,并一边研究一边做应用。其中遇到了把程序通知常驻在 Notification 栏,并且不能被 clear 掉(就像android QQ一样)的问题。经过研究实现了其功能,现把 Notification 的使用总结如下:

Notification 的使用需要导入 3 个类

1
2
3
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;

代码示例及说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);              
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());            
n.flags = Notification.FLAG_AUTO_CANCEL;               
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);          
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
        arg0.getContext(),
        R.string.app_name,
        i,
        PendingIntent.FLAG_UPDATE_CURRENT);
                 
n.setLatestEventInfo(
        arg0.getContext(),
        "Hello,there!",
        "Hello,there,I'm john.",
        contentIntent);
nm.notify(R.string.app_name, n);

下面依次对每一段代码进行分析:

1
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

创建 NotificationManager,其中创建的 nm 对象负责“发出”与“取消”  Notification。

1
2
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());            
n.flags = Notification.FLAG_ONGOING_EVENT; 

创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。其中创建的 n 对象用来描述出现在系统通知栏的信息,之后我们将会看到会在 n 对象上设置点击此条通知发出的Intent。

1
n.flags = Notification.FLAG_AUTO_CANCEL;

设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后,能够清除该通知。

1
2
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent

请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。

Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例

Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity。更多请参见 “ (转载)Android下Affinities和Task ”

1
2
3
4
5
6
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
        arg0.getContext(),
        R.string.app_name,
        i,
        PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位。

其中 PendingIntent.FLAG_UPDATE_CURRENT  表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。

这里再简要说一下 Intent 与 PendingIntent 的区别:

Intent :意图,即告诉系统我要干什么,然后系统根据这个Intent做对应的事。如startActivity相当于发送消息,而Intent是消息的内容。

PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast 启动某项工作的意图。而某些时候,我们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发出一条Activity 的 Intent 。因此如果我们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity ,startService 还是 sendBroadcast 来启动Intent 的(当然还有其他的“描述”),因此这里便需要PendingIntent。

1
2
3
4
5
n.setLatestEventInfo(
        arg0.getContext(),
        "Hello,there!",
        "Hello,there,I'm john.",
        contentIntent);

设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent。

1
nm.notify(R.string.app_name, n);

启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification。

如何使自己的Notification像Android QQ一样能出现在 “正在运行的”栏目下面

其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。

如何改变 Notification 在“正在运行的”栏目下面的布局

创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
PendingIntent contentIntent = PendingIntent.getActivity(
    arg0.getContext(),
    R.string.app_name,
    i,
    PendingIntent.FLAG_UPDATE_CURRENT);
             
RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I'm john.");
n.contentView = rv;
n.contentIntent = contentIntent;
nm.notify(R.string.app_name, n);

注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,因为在最后一句绑定的时候,之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。

android Notification 的使用相关推荐

  1. android Notification的使用

    今天,简单讲讲android里如何使Notification. 之前,我讲如何使用服务器进行版本升级时提到了Notification.这个其实我并不常用,所以当时看代码时也是查找了资料,这个很多地方还 ...

  2. Android Notification实现推送消息过程中接受到消息端有声音及震动及亮屏提示

    在Android Notification状态栏通知一文中,简单实现了消息的推送效果,这里就接着上文说一下,当用户接受到消息时的提示效果 // 5-加入震动及声音及亮屏 notification.de ...

  3. Android Notification状态栏通知

    没有增加额外的震动及声音效果,这里直接实现了通知的功能,看效果吧: MainActivity.java package com.example.notification;import android. ...

  4. Android Notification总结

    Android Notification总结 目录[-] 一.通知的主要功能 二.通知简介 三.通知的使用流程 四.使用NotificationCompat.Builder设置通知的属性: 五.管 ...

  5. Android Notification通知详解

    Android Notification通知详解 Notification: (一).简介: 显示在手机状态栏的通知.Notification所代表的是一种具有全局效果的通知,程序一般通过Notifi ...

  6. android notification应用之自定义来电通知

    android notification应用之自定义来电通知 1.为了实现老板的各种要求 本人矜矜业业完成任务 随着这个软电话软件的日益完善 本来来电的时候是创建一条通知点亮屏幕 用户可以解锁屏幕后接 ...

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

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

  8. Android Notification通知详细解释

    Android Notification通知具体解释 Notification: (一).简单介绍: 显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通知,程序一般通过N ...

  9. android系统通知栏的弹框流程,Android Notification 手机系统横幅弹出提示框调用

    类似于仿微信信息提示提出框. 在项目当中集成了推送功能,当手机接收到消息后只是在手机通知栏有提示信息.所以需要展示像微信信息弹出框一样的效果,开始自己还以为微信信息弹出框是自定义Dialog之类的自定 ...

  10. Android Notification 详解

    下图是我对 Notification 做的思维导图,也是本文的主要逻辑.  本文主要讲述 Notification 的基本操作部分,进阶部分的内容还在学习ing~ Notification 概述 N ...

最新文章

  1. asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”...
  2. Windows8 正式版最简单的去除桌面水印方法
  3. 【OJ】OJ的介绍和常用OJ推荐
  4. [Swift]最强UIButton解析 | #selector()绑定点击事件
  5. 两种方法清除Excel保护密码
  6. 华为鸿蒙汽车自动驾驶,华为鸿蒙车机OS现身,自动驾驶再进化(一)
  7. C# list删除 另外list里面的元素_C#并发实战Parallel.ForEach使用
  8. eclipse修改字体大小
  9. Halcon例程学习之距离变换(distance_transform)
  10. Vue Cli 3 搭建一个可按需引入组件的组件库架子
  11. python画图x轴时间间隔_matplotlib绘图-设置横坐标为日期显示范围与间隔
  12. 计算机网络布线开题报告,网络综合布线开题报告.docx
  13. 51单片机连接ESP8266串口WiFi模块
  14. 数据管理:业务数据清洗,落地实现方案
  15. 哪里有免费的ASP空间?
  16. 下载微信账单用于分析
  17. R语言 by()用法
  18. 阿里云服务器搭建java运行环境(jdk+mysql+tomcat)
  19. post请求302以及post请求变更为get请求的问题排查小记
  20. 漏洞复现篇——利用XSS漏洞实现键盘记录

热门文章

  1. MFC的Application Wizard所生成的各种文件功能
  2. 大数据之-Hadoop3.x_MapReduce_TextInputFormat---大数据之hadoop3.x工作笔记0107
  3. vue中this.$nextTick()的使用---SpringCloud Alibaba_若依微服务框架改造_ElementUI---工作笔记017
  4. ES11新特性_动态import---JavaScript_ECMAScript_ES6-ES11新特性工作笔记065
  5. STM32工作笔记0010---认识GPIO IO端口
  6. Linux工作笔记035---linux内网测试访问外网网速_外网测试访问内网网速
  7. Android学习笔记---26_网络通信之资讯客户端,使用pull解析器,解析,从网络中获得的自定义xml文件
  8. pytorch 训练face出现的问题
  9. 基于BS模式的航材电子商务交易平台(2)
  10. 迄今为止我所见过的将BP算法最好的PPT