通知(Notification)
Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。它是看不见的程序组件
(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
Notification类中的一些常量,字段,方法:
1. DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等
2. DEFAULT_LIGHTS 使用默认闪光提示
3. DEFAULT_SOUNDS 使用默认提示声音
4. DEFAULT_VIBRATE 使用默认手机震动
以上效果可以叠加,通过mNotifaction.defaults =DEFAULT_SOUND |
DEFAULT_VIBRATE;
设置flag位
1. FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
2. FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
3. FLAG_ONGOING_EVENT 通知放置在正在运行
4. FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
常用字段
1. contentIntent 设置PendingIntent对象,点击时发送该Intent,
2. defaults 添加默认效果
3. flags 设置flag位,例如FLAG_NO_CLEAR等
4. icon 设置图标
5. sound 设置声音
6. tickerText 显示在状态栏中的文字
7. when 发送此通知的时间戳
创建Notification
一个notification不必对上面所有的选项都进行设置,但有3项是必须的:
- 小图标 setSmallIcon()
- 内容标题 setContentTitle()
- 内容 setContentText()
NotificationManager
通知一般通过NotificationManager服务来发送一个Notification对象来完成,NotificationManager是一个重要的系统级服务,该对象位于应用程序的框架层中,应用程序可以通过它像系统发送全局的通知。这个时候需要创建一个Notification对象,用于承载通知的内容。
创建一个Notification的步骤简单可以分为以下四步:
实例化一个Notification.Builder对象;
注意:在 android:minSdkVersion <16以下时,可以使用NotificationCompat.Builder创建。
调用builder的相关方法对notification进行上面提到的各种字段
- 实例化一个NotificationManager对象;
- 调用manager的notify方法
注意: Notification也是使用 Builder模式 来创建的,它的静态方法已经过期。
所有的系统服务都是使用Context.getService()方法获取,如通知管理器,音量管理器,传感器管理器等等
自定义通知显示内容
1、 定义布局,如下:notification_layout.xml
这里用了两个ImageView 和TextView来显示通知的布局
<?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">
<ImageView
android:id="@+id/n_icon"
android:layout_width="60dp"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/n_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/n_icon"
android:text="ABC"
android:textColor="#FFFF0000"
android:textSize="20sp" />
<TextView
android:id="@+id/n_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/n_icon"
android:layout_alignLeft="@id/n_title"
android:text="ABC"
android:textColor="#FFffffff"
android:textSize="18sp" />
<ImageView
android:id="@+id/n_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/video_product_op_close"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
2、创建RemoteViews (远程视图管理)
// 创建一个远程视图管理,
RemoteViews contentView = new RemoteViews(getApplication().getPackageName(),R.layout.notification_layout);
contentView.setTextViewText(R.id.n_title,contentTitle);
contentView.setTextViewText(R.id.n_content,contentText); // 设置文本框中显示的文本内容
contentView.setImageViewResource(R.id.n_icon,R.drawable.noti_icon); // 设置图片视图
// 设置自定义布局中的点击监听
// 关闭的 Intent
Intent closeIntent = new Intent(this,MusicService.class);
closeIntent.putExtra("opt",1);
PendingIntent closePending = PendingIntent.getService(this,2,closeIntent,PendingIntent.FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.n_close,closePending);
3 、将 RemoteViews 设置到通知的内部布局上, 4.2 以上版本用 Builder 的 builder.setContent(contentView) 来设置
Notification.Builder builder = new Notification.Builder(this);
// 显示在状态栏上的小图标
builder.setSmallIcon(R.drawable.noti_icon);
// 添加状态栏上提示的文本 ( 显示一段时间就自动消失 )
builder.setTicker(ticker);
builder.setContent(contentView);
// 如果点击了内容部分,跳转到 Activity02 中
builder.setContentIntent(pIntent);
// 创建通知
notification = builder.build();
低版本
notification.contentView = contentView;
具体代码如下:
private void showNotification(String ticker, String contentTitle, String contentText) {
Notification notification;
Intent it = new Intent(this, Activity01.class);
//(Context对象,Intent的id,Intent对象,对Intent的更新方式)
//FLAG_UPDATE_CURRENT表示不销毁原来的PendingIntent,直接替换其中的Intent内容
//FLAG_CANCEL_CURRENT表示取消当前的PendingIntent,重新创建新的PendingIntent对象
PendingIntent pIntent = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
//创建一个远程视图管理
RemoteViews contentView = new RemoteViews(getApplication().getPackageName(), R.layout.notification_layout);
contentView.setTextViewText(R.id.n_title, contentTitle);
contentView.setTextViewText(R.id.n_content, contentText);//设置文本框中显示的文本内容
contentView.setImageViewResource(R.id.n_icon, R.drawable.not_icon);//设置图片视图
//设置自定义布局中的点击监听
//关闭的Intent,MusicService 进入onStartCommand中
Intent closeIntent = new Intent(this, MusicService.class);
closeIntent.putExtra("oop", 1);
PendingIntent closePdIntent = PendingIntent.getService(this, 2, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.n_close, closePdIntent);
if (Build.VERSION.SDK_INT >= 16) {
Notification.Builder builder = new Notification.Builder(this);
//显示在状态栏上的小图标
builder.setSmallIcon(R.drawable.icon);
//显示在状态栏上的提示文本(显示一段时间消失)
builder.setTicker(ticker);
// //下拉看到的内容标题和文本
// builder.setContentTitle(contentTitle);
// builder.setContentText(contentText);
// //通知时间
// builder.setWhen(System.currentTimeMillis());
builder.setContent(contentView);
//如果点击了内容部分,跳转到Activity01界面
builder.setContentIntent(pIntent);
//创建通知
notification = builder.build();
} else {
notification = new Notification(R.drawable.icon, ticker, System.currentTimeMillis());
notification.contentIntent = pIntent;
notification.contentView = contentView;
}
//自动取消(当通知被点击时,系统会移除通知)
notification.flags = notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
// 显示通知
nm.notify(333, notification);
}
//第一次启动服务,服务创建成功时触发
@Override
public void onCreate() {
Log.e("m_tag", "=====onCreate======");
super.onCreate();
//获取通知管理器
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
showNotification("这是一个通知", "今天你有好运", "你要遇上个小姐姐");
}
//启动端和服务端的通讯入口
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("m_tag", "=====onStartCommand======");
if (intent != null && intent.hasExtra("oop")) {
int oop = intent.getIntExtra("oop", 3);
if (oop == 1) {
//关闭
stopSelf();
}
}
return super.onStartCommand(intent, flags, startId);
}
//销毁服务时触发
@Override
public void onDestroy() {
Log.e("m_tag", "=====onDestroy======");
super.onDestroy();
nm.cancel(333);
}