Android_Notification通知状态栏详解

1.概述

通知消息显示在系统界面的指定位置,产生一个通知,首先会被显示在 the notification area,如果想查看具体的细节信息,可以滑开 the notification drawer.
注意: The NotificationCompat.Builderclass in the version 4 is Support Library. The classNotification.Builderwas added in Android 3.0.

2.主要元素

A notification in normal view appears in an area that's up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it's expanded.

(1).Content title

builder.setContentTitle("5 new messages");

(2).Large icon

builder.setLargeIcon(Bitmap icon);

 

Android_Notification通知状态栏详解_第1张图片

(3).Content text

builder.setContentText([email protected]);

(4).Content info

builder.setContentInfo("12");

(5).Small icon

builder.setSmallIcon(R.drawable.ic_launcher);

(6).Time

the notification was issued. You can set an explicit value withsetWhen(); if you don't it defaults to the time that the system received the notification

builder.setWhen(System.currentTimeMillis());

(7).Intent

3.创建Notification

对于一个通知可以通过NotificationCompat.Builder对象,为其指定UI信息和动作,而创建Notification实例对象,可以调用NotificationCompat.Builder.build()方法, 显示该对象调用NotificationManager.notify()去唤醒该Notification实例化对象!

(1).必须设置信息

1).A small icon, set by setSmallIcon()
2).A title, set by setContentTitle()
3).Detail text, set by setContentText()

All other notification settings and contents are optional.

(2).创建一个简单的Notification

public void notificationMethod() {
	
	//	1.创建一个NotificationCompat.Builder预对象
	NotificationCompat.Builder builder = new NotificationCompat.Builder(
			MainActivity.this);
	//	2.设置主要信息
	builder.setContentTitle("未读信息");						//设置内容题目
	builder.setContentText("您有" + (++num) + "封未读短信!");	//设置内容文本信息
	builder.setSmallIcon(R.drawable.ic_launcher);			//设置小图标
	builder.setContentInfo(String.valueOf(num)+"M");		//设置内容info
	builder.setTicker("您有未读短信!");							//对应的Notification对象create时,以消息在标题栏提示
	builder.setWhen(System.currentTimeMillis());			//设置时间
	builder.setAutoCancel(true);							//默认点击对应的notification对象后,该对象消失
	//	3.设置意图对象
	Intent intent = new Intent(MainActivity.this, Other.class);
	PendingIntent pendingIntent = PendingIntent.getActivity(
			MainActivity.this, 					// The Context in which this PendingIntent should start the activity.
			0, 									// request code
			intent, 							// Intent of the activity to be launched.
			PendingIntent.FLAG_UPDATE_CURRENT);	// 待补充...
	/*
	 * 隐含执行 MainActivity.this.startActivityForResult(intent, requestCode);
	 */
	builder.setContentIntent(pendingIntent);
	//	4.得到一个notification对象(根据builder预设置信息)
	Notification notification = builder.build();
	//	5.唤醒notification对象
	/*
	 * 		将该notification发送到状态条上,如果id相同且没有消失,则直接更新该notification对象信息
	 * 		否则创建一个Notification实例对象
	 */
	manager.notify(0, 		// int id 应用唯一值
			notification);	// Notification notification 不得设置为null
}

4.Notification管理

(1).更新notification

调用NotificationManager.notify(ID, notification),将该notification传递给status bar,根据ID完成该对象的创建和更新。如果存在相同id的notification并处于可见状态,系统将更新该notification。如果存在相同id的notification但是该notification已经消失, 那么将创建一个notification对象。

//将该notification发送到状态条上,如果id相同且没有消失,则直接更新该notification对象信息,否则创建一个Notification实例对象
manager.notify(0, 		// int id 应用唯一值
		notification);	// Notification notification 不得设置为null

(2).移除notification

1).The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).

//设置该notification不允许清除掉(the notification can't be cleared)
notification.flags = Notification.FLAG_NO_CLEAR;

2).The user clicks the notification, and you called setAutoCancel()when you created the notification.
3).You call cancel(int id) for a specific notification ID. This method also deletes ongoing notifications.
4).You call cancelAll(), which removes all of the notifications you previously issued.

 

 

 

你可能感兴趣的:(android,notification)