状态栏消息提示——使用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 ...
随机推荐
- 【代码笔记】iOS-获取系统完成任务所需的后台时间
一,代码. AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplica ...
- iOS开发之功能模块--推送之坑问题解决
不管想不想看我后面再开发中总结的经验,但是很值得推荐一位大神写的关于苹果推送,很多内容哦:http://www.cnblogs.com/qiqibo/category/408304.html 苹果开发 ...
- Python绘制PDF文件~超简单的小程序
Python绘制PDF文件 项目简介 这次项目很简单,本次项目课,代码不超过40行,主要是使用 urllib和reportlab模块,来生成一个pdf文件. reportlab官方文档 http:// ...
- js操作数组
一.数组的声明方式: var colors = new Array();//创建数组 var colors = new Array(20);//创建20个长度的数组 var colors = new ...
- .NET 4.5 中新提供的压缩类
Windows8 的开发已经如火如荼开始了,在 Windows8 中提供的 .NET Framework 已经更新到了 4.5 版,其中又增加了一些新的特性,对压缩文件的支持就是其中之一. 在 4.5 ...
- MongoDB-分片片键
1.分片 分片是什么?分片就是将数据存储在多个机器上.当数据集超过单台服务器的容量,服务器的内存,磁盘IO都会有问题,即超过单台服务器的性能瓶颈.此时有两种解决方案,垂直扩展和水平扩展(分片). ...
- 还来一篇说下json_value 以及 json_query 的应用 (3)
上一篇说了一下openjson 的一些使用方法,这次再说一下在2016里面的查询取值,当然就是 json_query 和 json_value 这2兄弟了 首先 ) = '{"a" ...
- java请求https地址如何绕过证书验证?
原文http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两种 ...
- 记SQL SERVER一次诡异的优化
最近做的项目快上线了,在做了一次压力测试后发现了不少问题,基本都是因为数据量达到一定级别时(预测系统上线10年后的数据量)SQL查询超时,其中有些是因为索引缺失.有些是因为写法不好,这些在有经验的人眼 ...
- Win10系统旗舰版ghost版系统镜像下载
微软已经发布了Win10预览版10041快速版更新,但通过Windows更新的方式比较慢.现在微软官方已经发布Win10预览版10041的系统ISO镜像,还没更新这一版本的朋友可以使用该镜像进行更新. ...