状态栏消息提示——使用Notification
什么是Notification
Notification用于在状态栏显示信息。这些信息一般来源于app的消息推送,或应用的一些功能控制(如播放器)
Notification的两种视图
普通视图

借用官方的图片说明一下Notification视图中包括的内容
1. 内容标题
2. 大图标(Bitmap)
3. 正文内容
4. 附加信息
5. 小图标
6. Notification的推送时间
大视图

除了上图中标出的第7点外,其他的基本和普通视图中的一样。
其中第7点可显示为一下几种内容
1. 显示大图片
在详情区域可以显示一张最大为256dp的图片
2. 显示长文本
在详情区域显示长文本
3. inbox style(因为不知道中文怎样才能说得准确,这里用英文表达)
inbox style在详情区域显示多行文本.
4. 大内容标题
允许用户为扩展视图定义另一个标题。
5. 摘要文本
允许用户在详情区域添加一行额外的文本
创建标准的Notification
以下三项式创建一个标准的Notification的先决条件
1. 小图标, 通过setSmallIcon()方法设置
2. 标题, 通过setContentTitle()方法设置
3. 内容文本, 通过setContentText()方法设置
我们这里不通过调用Notification构造方法的方式创建Notification,因为这种方法已经不推荐使用。
我们使用通过NotificationCompat.Builder类创建Notification。
程序的主布局文本中只有一个id为btnShow的按钮,点击这个按钮显示Notification
package com.whathecode.notificationdemo; import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button; public class MainActivity extends ActionBarActivity { private Notification mNotification;
private NotificationManager mNotificationManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnShow = (Button) findViewById(R.id.btnShow); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotification = new NotificationCompat.Builder(this)
// 设置小图标
.setSmallIcon(R.drawable.ic_launcher)
// 设置标题
.setContentTitle("you have a meeting")
// 设置内容
.setContentText("you have a meeting at 3:00 this afternoon")
.build(); btnShow.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
mNotificationManager.notify(0, mNotification);
}
});
}
}
运行效果

当然,这个Demo在4.x系统上是可以正常运行的。
当同样的代码在2.x的系统上面运行,程序会崩溃的。如下

而且,会抛出一个异常:
java.lang.IllegalArgumentException: contentIntent required: pkg=com.whathecode.notificationdemo id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
上面的错误已经说得很清楚了,contentIntent required , 也就是需要contentIntent
这个contentIntent可以通过setContentIntent设置。
也就是说,上面所提到的三个先决条件只是确保4.x版本可以正常运行,
如果你的程序需要兼容2.x版本的,你就需要一个contentIntent
contentIntent的作用是点击Notification时跳转到其他的Activity
更新后的代码:
package com.whathecode.notificationdemo; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button; public class MainActivity extends ActionBarActivity { private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mResultIntent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnShow = (Button) findViewById(R.id.btnShow); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); /*
* 取得PendingIntent,
* 因为我们现在不需要跳转到其他Activity,
* 所以这里只实例化一个空的Intent
*/
Intent intent = new Intent();
mResultIntent = PendingIntent.getActivity(this, 1, intent,
Intent.FLAG_ACTIVITY_NEW_TASK); mNotification = new NotificationCompat.Builder(this)
// 设置小图标
.setSmallIcon(R.drawable.ic_launcher)
// 设置标题
.setContentTitle("系统更新")
// 设置内容
.setContentText("发现系统更新,点击查看详情")
//设置ContentIntent
.setContentIntent(mResultIntent)
.build(); btnShow.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
mNotificationManager.notify(0, mNotification);
}
});
}
}
运行效果

完整的Notification实例
上面代码中的例子并不完整,只是演示了一个可以正常运行的实例
说它不完整是因为Notification到来的时候状态栏只有图标,没有文字,也没有声音提示,用户体验并不好。
当我们不设置LargeIcon的时候,系统就会使用当前设置的小图标作为大图标使用,小图标位置(位置5)则置空。
下面的代码是比较完整的Notification示例
package com.whathecode.notificationdemo; import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button; public class MainActivity extends ActionBarActivity { private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mResultIntent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnShow = (Button) findViewById(R.id.btnShow); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); /*
* 取得PendingIntent, 并设置跳转到的Activity,
*/
Intent intent = new Intent(this, UpdateActivity.class);
mResultIntent = PendingIntent.getActivity(this, 1, intent,
Intent.FLAG_ACTIVITY_NEW_TASK); btnShow.setOnClickListener(new View.OnClickListener() { @SuppressLint("NewApi")
@Override
public void onClick(View v) { Bitmap largeIcon = BitmapFactory.decodeResource(
MainActivity.this.getResources(), R.drawable.luffy); mNotification = new NotificationCompat.Builder(getBaseContext()) // 设置大图标
.setLargeIcon(largeIcon) // 设置小图标
.setSmallIcon(R.drawable.ic_launcher) // 设置小图标旁的文本信息
.setContentInfo("1") // 设置状态栏文本标题
.setTicker("你的系统有更新") // 设置标题
.setContentTitle("系统更新") // 设置内容
.setContentText("发现系统更新,点击查看详情") // 设置ContentIntent
.setContentIntent(mResultIntent) // 设置Notification提示铃声为系统默认铃声
.setSound(
RingtoneManager.getActualDefaultRingtoneUri(
getBaseContext(),
RingtoneManager.TYPE_NOTIFICATION)) // 点击Notification的时候使它自动消失
.setAutoCancel(true).build(); mNotificationManager.notify(0, mNotification);
}
}); }
}
运行效果(Android 2.x) 运行效果(Android 4.x)

通过观察上面两张图你会发现,在2.x版本下LargeIcon和contentInfo并没有生效。
这是因为setLargeIcon和setContentInfo都是在api level 11(honeycomb)才添加的功能
使用自定义布局统一Notification的显示效果
布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"> <ImageView
android:id="@+id/largeIcon"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/luffy"
android:contentDescription="largeIcon" /> <TextView
android:id="@+id/contentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/largeIcon"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="系统更新"/> <TextView
android:id="@+id/contentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/contentTitle"
android:layout_below="@+id/contentTitle"
android:layout_marginTop="5dp"
android:text="发现系统更新,点击查看详情"
android:textSize="12sp"/> <TextView
android:id="@+id/when"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="11:00"/> <ImageView
android:id="@+id/smallIcon"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignRight="@+id/when"
android:layout_alignTop="@+id/contentText"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/contentInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/smallIcon"
android:layout_toLeftOf="@+id/smallIcon"
android:text="信息"
android:textSize="12sp" /> </RelativeLayout>
程序代码:
package com.whathecode.notificationdemo; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews; public class MainActivity extends Activity { private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mResultIntent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnShow = (Button) findViewById(R.id.btnShow); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); /*
* 取得PendingIntent, 并设置跳转到的Activity,
*/
Intent intent = new Intent(this, UpdateActivity.class);
mResultIntent = PendingIntent.getActivity(this, 1, intent,
Intent.FLAG_ACTIVITY_NEW_TASK); btnShow.setOnClickListener(new View.OnClickListener() { @SuppressLint("NewApi")
@Override
public void onClick(View v) { RemoteViews customView = new RemoteViews(getPackageName(),
R.layout.customerviews); mNotification = new NotificationCompat.Builder(getBaseContext()) // 设置小图标
.setSmallIcon(R.drawable.ic_launcher) // 设置状态栏文本标题
.setTicker("你的系统有更新") //设置自定义布局
.setContent(customView) // 设置ContentIntent
.setContentIntent(mResultIntent) // 设置Notification提示铃声为系统默认铃声
.setSound(
RingtoneManager.getActualDefaultRingtoneUri(
getBaseContext(),
RingtoneManager.TYPE_NOTIFICATION)) // 点击Notification的时候自动移除
.setAutoCancel(true).build(); /*
* 当API level 低于14的时候使用setContent()方法是没有用的
* 需要对contentView字段直接赋值才能生效
*/
if (Build.VERSION.SDK_INT < 14) {
mNotification.contentView = customView;
} mNotificationManager.notify(0, mNotification);
}
}); }
}
上面的布局文件中的内容是硬编码的,也可以通过RemoteViews类中的setXXX方法动态设定内容。
这样程序便更加灵活
运行效果(android 2.x) 运行效果(android 4.x)

在两个版本中的运行效果基本一样了,但是,在低版本下不是很完美,有部分灰白的地方特别扎眼
网上查了很久也没有找到解决办法,有知道的请告知
更新Notification的内容
因为我们创建的Notification是通过一个Id分别的,因此只要在使用NotificationManager的notify()方法的时候使用
相同的Id就可以更新这个Notification的内容
当然在使用notify()方法之前我们还是要创建一个Notification实例。
状态栏消息提示——使用Notification的更多相关文章
- 第13讲- Android之消息提示Notification
第13讲 Android之消息提示Notification .Notification Notification可以理解为通知的意思一般用来显示广播信息,通知可以显示到系统的上方的状态栏(status ...
- Android实现系统下拉栏的消息提示——Notification
Android实现系统下拉栏的消息提示--Notification 系统默认样式 默认通知(通用) 效果图 按钮 <Button android:layout_width="match ...
- 在vue项目中的main.js中直接使用element-ui中的Message 消息提示、MessageBox 弹框、Notification 通知
需求来源:向后台请求数据时后台挂掉了,后台响应就出现错误,不做处理界面就卡住了,这时需要在main.js中使用axios的响应拦截器在出现相应错误是给出提示.项目使用element-ui,就调用里面的 ...
- Android三种消息提示
Android消息提示有三种方式: 1 使用Toast显示消息提示框 Toast类用于在屏幕中显示一个提示信息框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一定时间后自动消失.通常用于显示 ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- AutoCompleteTextView,Spinner,消息提示
package com.example.wang.testapp2; import android.app.Notification; import android.app.NotificationM ...
- android中提示&对话框----Notification
Notification(状态栏通知) 一.Notification用于状态栏显示通知的控件,在不同的设备上面Notification是不一样的 二.Notification的基本布局 元素组成: I ...
- Android——AutoCompleteTextView、Spinner和消息提示
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- React Native之通知栏消息提示(ios)
React Native之通知栏消息提示(ios) 一,需求分析与概述 详情请查看:React Native之通知栏消息提示(android) 二,极光推送注册与集成 2.1,注册 详情请查看:Rea ...
随机推荐
- 自定义加载loading view动画组件的使用。
在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress 我写写使用步骤 自定义view(CircleProgress ...
- Android 手机卫士--设置界面&功能列表界面跳转逻辑处理
在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...
- c++ 奇特的递归模板模式(CRTP)
概述 使用派生类作为模板参数特化基类. 与多态的区别 多态是动态绑定(运行时绑定),CRTP是静态绑定(编译时绑定) 在实现多态时,需要重写虚函数,因而这是运行时绑定的操作. CRTP在编译期确定通过 ...
- .NET并行编程实践(一:.NET并行计算基本介绍、并行循环使用模式)
阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLINQ) 1]开篇介绍 最近这几天在捣鼓并行计算 ...
- Python检查xpath和csspath表达式是否合法
在做一个可视化配置爬虫项目时,需要配置爬虫的用户自己输入xpath和csspath路径以提取数据或做浏览器操作.考虑到用户的有时会输入错误的xpath或csspath路径,后台需要对其做合法性校验. ...
- 【转】Flex 布局语法教程
网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中 ...
- FastReport自定义数据源及ListView控件的使用
##1.想批量生成一堆物资信息卡,效果如下图所示,fastreport可以一下全部生成,并且发现不用单独写东西, ##2.发现FastReport官方给出的Demo.exe很友好,基本可以满足要求,想 ...
- UVALive 4428 Solar Eclipse --计算几何,圆相交
题意:平面上有一些半径为R的圆,现在要在满足不与现有圆相交的条件下放入一个圆,求这个圆能放的位置的圆心到原点的最短距离. 解法:我们将半径扩大一倍,R = 2*R,那么在每个圆上或圆外的位置都可以放圆 ...
- 第54课 Qt 中的多页面切换组件
1. 多页面切换组件(QTabWidget) (1)能够在同一个窗口中自由切换不同页面的内容 (2)是一个容器类型的组件,同时提供友好的页面切换方式 2. QTabWidget的使用方式 (1)在应用 ...
- thinkphp发送邮件
看thinkPHP手册发送邮件 Thinkphp3.2 PHPMailer 发送邮件结合QQ企业邮箱发送邮件下载附件PHPMailer解压到ThinkPHP\Library\VendorPHPMail ...