手机通知栏主要设计的类:
Notification、NotificationManager、Intent、PendingIntent、TaskStackBuilder Action、RemoteViews
1、Notification类没有直接使用,通知栏与弹窗使用了相同的设计模式------工厂模式
new
Notification.Builder(MainActivity.
this)
.setSmallIcon(R.mipmap.
ic_launcher
)
.setContentTitle(
"My Notification"
)
.setContentText(
"hello world"
);
通过使用Notification.Builder中的build方法生成一个Notification。
2、Intent负责使用在点击时调用指定的Activity
在Notification中只能设定PendingIntent,需要根据TaskStackBuilder构建一个任务栈,并向任务栈中添加下一个任务的Intent
通过使用TaskStackBuiler来获取一个PendingIntent,
Intent resultTintent = new Intent(this,MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultTintent);
PendingIntent sendIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
3、NotificationManager负责Notification的添加和移除
在添加Notification时需要制定当前添加Notification的Id,重复添加相同Id的Notification时,如果原有的Notification没有被销毁,
将不再创建新的Notification,只是更新Notification的内容。
个人感觉:对于NotificationManager而言,使用Notification的Id表示是否是同一个Notification。
NotificationManager没有提供获取Notification的Id的方法,但是可以获取当前活动的Notification的数组,通过对数组进行遍历可以获取
指定的Notification。
mManager.notify(99,mBuilder.build());
mManager.cancel(99);
4、RemoteViews用于自定义Notification的布局。
通过使用Notification.Builder的setContentView方法添加自定义的布局。
在使用RemoteViews进行自定义布局时,需要另外指定Notification的smallIcon,不然会报错。
自定义通知布局的可用高度取决于通知视图。普通视图布局限制为 64 dp,扩展视图布局限制为 256 dp。
要定义自定义通知布局,请首先实例化 RemoteViews 对象来扩充 XML 布局文件。然后,调用 setContent(),而不是调用 setContentTitle() 等方法。要在自定义通知中设置内容详细信息,请使用 RemoteViews 中的方法设置视图子项的值:
1. 在单独的文件中为通知创建 XML 布局。您可以根据需要使用任何文件名,但必须使用扩展名
.xml。
2. 在您的应用中,使用
RemoteViews 方法定义通知的图标和文本。通过调用
setContent() 将此
RemoteViews 对象放入
NotificationCompat.Builder中。
5、设置Notification的可见性
调用 setVisibility() 并指定以下值之一:
-
VISIBILITY_PUBLIC 显示通知的完整内容。
-
VISIBILITY_SECRET 不会在锁定屏幕上显示此通知的任何部分。
-
VISIBILITY_PRIVATE 显示通知图标和内容标题等基本信息,但是隐藏通知的完整内容。
设置 VISIBILITY_PRIVATE 后,您还可以提供其中隐藏了某些详细信息的替换版本通知内容。例如,短信 应用可能会显示一条通知,指出“您有 3 条新短信”,但是隐藏了短信内容和发件人。要提供此替换版本的通知,请先使用 NotificationCompat.Builder 创建替换通知。创建专用通知对象时,请通过setPublicVersion() 方法为其附加替换通知。
6、在Notification中显示进度条,显示音乐播放控制控件:
mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder =newNotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
newThread(
newRunnable(){
@Override
publicvoid run(){
int incr;
// Do the "lengthy" operation 20 times
for(incr =0; incr <=100; incr+=5){
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
// state
mBuilder.setProgress(100, incr,false);
// Displays the progress bar for the first time.
mNotifyManager.notify(0, mBuilder.build());
// Sleeps the thread, simulating an operation
// that takes time
try{
// Sleep for 5 seconds
Thread.sleep(5*1000);
}catch(InterruptedException e){
Log.d(TAG,"sleep failure");
}
}
// When the loop is finished, updates the notification
mBuilder.setContentText("Download complete")
// Removes the progress bar
.setProgress(0,0,false);
mNotifyManager.notify(ID, mBuilder.build());
}
}
// Starts the thread by calling the run() method in its Runnable
).start();
setProgress( 0 ,0, false );相当于清除进度条setProgress(100,i,false);--- i != 0时当设置为true时,之前绘制的进度条会被清除,设置为false则不会清除。
setProgress(0,0,true);生成一个不断循环滚动的进度条