当后台服务需要提示用户来响应某个事件时,应该使用状态栏通知。后台服务不应该自己去启动一个activity来与用户互动,它应该创建一个状态栏通知,当用户选择这个通知去启动activity
创建一个普通的通知栏
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder
.setSmallIcon(R.drawable.tubiao) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null)) .setWhen(System.currentTimeMillis())
.setContentTitle("this is title")
.setContentText("this is content")
.build();
manager.notify(1,notification);
效果:
这是一个最普通的通知栏,一般都会为通知添加至少一个动作(Action),这个动作可以是跳转到Activity、启动一个Service或发送一个Broadcas等。 通过以下方式为通知添加动作:
使用PendingIntent
通过大视图通知的 Action Button //仅支持Android 4.1 (API level 16)及更高版本
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this,geciActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,0);
Notification notification = builder.setSmallIcon(R.drawable.tubiao) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null)) .setWhen(System.currentTimeMillis())
.setContentTitle("this is title")
.setContentText("this is content") .setContentIntent(pendingIntent)
.build();
manager.notify(1,notification);
更新通知
更新通知很简单,只需再次发送相同ID的通知即可,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建。
Notification notification2 =
builder.setSmallIcon(R.drawable.tubiao)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null))
.setWhen(System.currentTimeMillis())
.setContentTitle("this is title22222")
.setContentText("this is content222222")
.setContentIntent(pendingIntent)
.build();
manager.notify(1,notification2);
效果:
取消通知:
取消通知有如下4种方式:
1. 点击通知栏的清除按钮,会清除所有可清除的通知0
2. 设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
3. 通过 NotificationManager 调用 cancel() 方法清除指定ID的通知
4. 通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
大视图通知:
通知有两个类型,一种是普通视图,一种是大视图:
普通视图:
大视图:
默认情况下是普通视图,可以通过NotificationCompat.Bulider.setStyle()设置大视图
例子:
Notification notification3 =
builder.setSmallIcon(R.drawable.tubiao)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null))
.setContentTitle("this is title2222233333")
.setContentText("this is content22222233333")
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null)))
.setStyle(new NotificationCompat.BigTextStyle().bigText("ddsfsdvdvddadaergadvadfaasdferavadgarvaergadvaergvasgqrefaerhtrrthbsdbsrtgadgw")) .addAction(R.drawable.zuo,null,pendingIntent) .addAction(R.drawable.you,null,pendingIntent)
.build();
manager.notify(1,notification3);
效果:
这个大视图是通过双手上下平移展开的。
进度条通知
- 明确进度的进度条
使用setProgress(max, progress, false)来更新进度。
max: 最大进度值
progress: 当前进度
false: 是否是不明确的进度条
模拟下载过程:
int id = 1;
...
mNotifyManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
new Runnable() {