一:普通Notification

 1.内容标题setContentTitle(...)
 2.大图标setLargeIcon(Bitmap)
 3.内容setContentText(...)
 4.内容附加信息setContentInfo(...)
 5.小图标setSmallIcon(...)
 6.时间(自动生成)

二:Notification的创建

1>实例化一个NotificationCompat.Builder对象
2>调用builder的相关方法对notification设置标题,图标,内容等;
3>调用builder.build()方法,此方法返回一个Notification类的对象;
4>实例化一个NotificationManager对象
5>调用manager的notify(...)方法 来发送通知。

三:普通的Notification(点击之后跳到打电话界面,不需要可以不写setContentIntent这个方法)

private NotificationManager notificationManager;
// 获取通知管理器的系统组件实例
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
private void showNormal() {
//建造者模式----利用静态内部类来构建宿主类的对象,链式编程
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("ContentTitle")//设置通知标题
.setContentText("ContentText")//设置通知内容
.setTicker("showNormal")//设置滚动字幕
.setContentInfo("contentInfo")//设置附加信息
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)//声音|震动通知,设置震动的时候需要设置权限: <uses-permission android:name="android.permission.VIBRATE" />
//DEFAULT_ALL:声音,震动,呼吸灯都有
.setContentIntent(PendingIntent.getActivity(this, 100,//100是requestCode
new Intent(Intent.ACTION_CALL,
Uri.parse("tel:10086")),
PendingIntent.FLAG_ONE_SHOT))//需要添加打电话权限,否则不会报错但无效果(延时意图不能在logCat中查看),如果没设置延时意图, 则点击通知无法自动消除.
.build(); //构建Notification 类对象
//注意:是notification对象的flags,不是Builder对象的
notification.flags |= Notification.FLAG_AUTO_CANCEL; //设置为点击后可清除,否则点击无法清除[必须同时设置了延时意图PendingIntent]
notification.flags |= Notification.FLAG_NO_CLEAR; //设置为不能横向滑动清除,否则横向拖动可清除,等价于builder对象调用.setOngoing(true)
notificationManager.notify(1, notification); //指定id,发送通知
}

四:带进度条的Notification

 private void showProgressBar() {
//final:因为要在内部类里面使用builder .
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("showProgressBar").setContentInfo("contentInfo")
.setOngoing(true) //不可拖动清除
.setContentTitle("ContentTitle")
.setContentText("ContentText");
//启动一个线程来更新进度,因为不能阻塞UI线程
new Thread(new Runnable() {//Thread构造方法的实参是一个匿名内部类(实现了Runnable接口)
@Override
public void run() {
for (int progress = 0; progress < 100; progress += 10) {
//第一个参数 最大进度, 第二个参数 当前进度
//第三个参数 无明确进度的进度条样式; true:不确定;false:确定
builder.setProgress(100, progress, false);
notificationManager.notify(2, builder.build());//发送通知
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("sleep failure");
}
}
builder.setContentTitle("下载完成")
.setProgress(0, 0, false).setOngoing(true);//设置为下载完成后可清除
notificationManager.notify(2, builder.build());
}
}).start();
}

五:返回应用的某个界面[了解]

有时候我们可能需要实现这样的功能:当新notification出现时,我们希望点击它后可直接进入应用相应的界面中去完整查看或处理此消息的功能。 然后,当我们点击back按钮时返回到应用主界面而不是桌面。比如:当我们有新的短信来时,我们在任务栏中点击它后进入读信息页面,当我们读完短信后,按“返回”键回到短信的主界面,而不是手机桌面。要实现这样的功能要我们做相应的处理:
private void backToApp() {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(OtherActivity.class);
// Adds the Intent to the top of the stack
Intent resultIntent = new Intent(this, OtherActivity.class);//点击通知后会打开OtherActivity,回退后显示MainActivity,再回退后退出app
//并在清单文件中配置OtherActivity依赖的父窗体为MainActivity(详见下面xml配置)
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,//int requestCode
PendingIntent.FLAG_UPDATE_CURRENT);//如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新 的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。

Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
.setTicker("backApp").setContentInfo("contentInfo")
.setContentTitle("ContentTitle").setContentText("ContentText")
.setContentIntent(resultPendingIntent).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL).build();
manager.notify(5, notification);
this.finish();//关闭当前Activity
}

并需要我们在配置文件中对我们用来显示详细信息的OtherActivity进行相应的配置如下:

<activity
android:name=".OtherActivity"
android:label="第二个页面"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>

Notification的更多相关文章

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

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

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

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

  3. android Notification介绍

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

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

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

  5. Missing Push Notification Entitlement 问题

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

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

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

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

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

  8. Android中使用Notification实现进度通知栏(Notification示例三)

    我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能.实现效果如下: 在 ...

  9. Android中使用Notification实现宽视图通知栏(Notification示例二)

    Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...

  10. Android中使用Notification实现普通通知栏(Notification示例一)

    Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...

随机推荐

  1. 关于TxQBService报的错,腾讯你真牛B啊

    腾讯你真牛B啊,浏览器都7了,还特么的报这么低级的错误,还每10秒写一条windows日志,让人有什么心情用你的浏览器,滚.

  2. 阿里2014校招笔试题(南大)——利用thread和sleep生成字符串的伪随机序列

    引言:题目具体描述记不大清了,大概是:Linux平台,利用线程调度的随机性和sleep的不准确性,生成一个各位均不相同的字符数组的伪随机序列.不得使用任何库函数.(这句记得清楚,当时在想线程库算不算, ...

  3. KV6.60 SP1

    组态王6.60 SP1全新发布! 组态王6.60 SP1对过去几年6系列中已解决过的故障进行了合并,包括各主线分支.各OEM版本中的故障总计122个,覆盖运行系统.开发系统.历史趋势曲线控件.报表.A ...

  4. 【linux】学习7

    鸟哥 22章内容 单个源代码编译运行 设有一个hello.c 可以用下面方式运行 生成可执行文件a.out [localhost scripts]$ gcc hello.c [localhost sc ...

  5. 更新补丁Bind

    1.查询补丁版本信息 (1) rpm -qa|grep bind (2) dig @localhost version.bind 2.下载安装 BIND最新漏洞和升级解决办法 现在有非常多的公司的都有 ...

  6. struts.xml配置

    1. package标签 package:完成有业务相关的Action(应用控制器的)管理 name:给包起的名字(反映该包中Action的功能),用来完成包和包之间的继承.默认继承struts-de ...

  7. nVivo highlight code中的文本

    要highlight nvivo中的code一颗在如图highlight中下拉菜单选择,如coding for all nodes,所有的有归属code的文本都会被高亮.如果选择coding for ...

  8. Linux系统下设置Tomcat自启动

    需要将tomcat加入自启动队列中,则需要进行如下的操作: 以root用户登录系统: cd /etc/rc.d/init.d/ vi tomcat 文件内容参考如下: #!/bin/sh # # to ...

  9. jQuery中读取json文件示例代码

    json文件是一种轻量级的数据交互格式.一般在jquery中使用getJSON()方法读取,具体示例代码如下,感兴趣的朋友可以参考下哈,希望可以帮助到你   json文件是一种轻量级的数据交互格式.一 ...

  10. mysql扩展库-1

    启用mysql扩展库 在php.ini文件中去配置mysql扩展库 extension=php_mysql.dll 可以通过 phpinfo() 查看当前php支持什么扩展库. 在sql扩展库中创建一 ...