状态栏消息提示——使用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 ...
随机推荐
- 最大似然判别法和Bayes公式判别法
最大似然判别法 Bayes公式判别法
- Emacs学习心得之 LaTeX编辑
目录 1. 前言 2. texlive的安装 3. AUCTEX的安装和配置 4. RefTEX的安装和配置 一.前言 本篇博文记录了Emacs下LaTeX编辑环境的搭建,参考一下文章: http:/ ...
- VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
[提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...
- jQuery修改class属性和CSS样式
jQuery修改class属性和CSS样式 class属性修改 类属性即class属性,规定类名. 用类选择器规定样式的时候,需要为元素指定类名,即class属性的值. 注意每个HTML元素只有一个c ...
- Android开发学习——android存储
Android的存储 内部存储空间RAM内存:运行内存,相当于电脑的内存ROM内存:存储内存,相当于电脑的硬盘外部存储空间 SD卡:相当于电脑的移动硬盘 * 2.2之前,sd卡路径:sdcard ...
- 基于ntp的多服务器时间同步脚本
server服务器每1个小时更新一次时间,server服务器旗下同步服务器每5秒钟同步一次时间,同步服务器与外网不做交互,由server服务器进行同步时间.所以保证每一台同步服务器时间一致. #vi ...
- Android 四大组件之再论BroadCast
BroadCast 是android提供的跨进程通讯的有一利器. 1.异步执行onReceiver @Nullable public abstract Intent registerReceiver( ...
- 做的一个HTML表白页面
页面地址: http://myspace123.qiniudn.com/love/index.html 目录文件结构: index.html <html xmlns="http://w ...
- Linq专题之提高编码效率—— 第二篇 神一样的匿名类型
说起匿名类型,我们都知道这玩意都是为linq而生,而且匿名类型给我们带来的便利性大家在实战中应该都体会到了,特别适合于一次性使用,临时 使用这些场景,虽然说是匿名类型,也就是说是有类型的,只是匿名了而 ...
- js 输出二维数组的最大值
function num(arr){ max=a[0][0]; for (var i = 0; i < a.length; i++) { for (var j = 0; j< a[i].l ...