1 通知栏可以使用自定义布局,可以在自定义布局上添加按钮,跳转到指定的activity
2 通知栏的生命周期和应用的生命周期无关,应用退出后,通知栏可以继续存在,但应用卸载后,通知栏会消失
显示通知栏:
private void createNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(intent);
builder.setTicker(getResources()
.getString(R.string.custom_notification));
//通知栏未展开时显示的小图标
builder.setSmallIcon(R.drawable.ic_stat_custom);
builder.setAutoCancel(true);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_NO_CLEAR;
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.notifycation_layout);
Intent iintent = new Intent(MainActivity.this, Activity2.class);
PendingIntent stentIntent = PendingIntent.getActivity(this, 0,
iintent, PendingIntent.FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.notify_set,stentIntent);
final String time = DateFormat.getTimeInstance().format(new Date())
.toString();
final String text = getResources().getString(R.string.collapsed, time);
contentView.setTextViewText(R.id.textView, text);
notification.contentView = contentView;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(5270, notification);
}
取消通知栏:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(5270);
PS:使用android sdk自带的demo是了解android控件使用方法的效率最高的手段