状态栏消息提示——使用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 ...
随机推荐
- android 动画效果
动画资源 一.分类: (一).概要: 3.0以前,android支持两种动画模式,补间动画(tween animation),帧动画(frame animation),在android ...
- iOS程序破解——class-dump获取头文件
原文在此:http://www.cnblogs.com/mddblog/p/4942894.html 一.简述 class-dump顾名思义,是用来dump目标对象class信息的工具.它根据oc的r ...
- JDBC快速入门
/** * JDBC快速入门: * 1.导入jar包 数据库驱动 * 2.注册驱动 * 3.获取数据库连接对象 Connection * 4.定义sql语句 * 5.获取发送执行sql语句的对象 St ...
- iOS 数字滚动 类似于老 - 虎- 机的效果
效果图 具体实现代码如下 ZCWScrollNumView.h文件 #import <UIKit/UIKit.h> typedef enum { ZCWScrollNumAnimation ...
- 大数据系列(4)——Hadoop集群VSFTP和SecureCRT安装配置
前言 经过前三篇文章的介绍,已经通过VMware安装了Hadoop的集群环境,当然,我相信安装的过程肯定遇到或多或少的问题,这些都需要自己解决,解决的过程就是学习的过程,本篇的来介绍几个Hadoop环 ...
- 对B+树与索引在MySQL中的认识
[TOC] 概述 本质:数据库维护某种数据结构以某种方式引用(指向)数据 索引取舍原则:索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数 B树 满足的条件 d为大于1的一个正整数,称为B-Tre ...
- linuxx virutal machine installation
1. vmplayer download: https://my.vmware.com/web/vmware/free#desktop_end_user_computing/vmware_player ...
- execve(file, argv, env)参数argv获取字符串个数
/* Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation, Inc. This file is part of the GNU ...
- django url路径与模板中样式相对路径的问题
static目录下有css和js及image等文件夹,里面放置网站的一些静态文件,static位于网站根目录下,django中配置静态文件这个就细说,网上都有,昨天在添加新内容时发现一个问题,我的ur ...
- STM32电机控制器小心得
首先声明的是本人刚刚大学毕业进入电机控制这个行业,以前在学校也做过类似51的实验,然而在工作中发现那些东西是皮毛的不能再皮毛,我现在在公司也算是一个实习生,主要工作是改各厂家对控制器的功能需求,(其实 ...