通知栏可以设置很多样式,根据不同的需求可以设置不同的通知栏:
1 最普通的一种通知栏:
private void initNotification() {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("htt://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, 0);
Notification.Builder builder= new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
builder.setContentTitle("这是标题");
builder.setContentText("这是内容");
builder.setSubText("这是内容下面的文字");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(NOTIFICATION_ID_NUM1,notification);
}
2.设置一个折叠式的通知栏:
private void initNotification2() {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("htt://www.sina.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, 0);
Notification.Builder builder= new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.build();
<strong> notification.contentView =remoteViews;
notification.bigContentView = extendViews;</strong>
notificationManager.notify(NOTIFICATION_ID_NUM2,notification);
}
两种布局:可以自定义
remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
remoteViews.setTextViewText(R.id.tv_no, "我是内容");
extendViews = new RemoteViews(getPackageName(),R.layout.extendview);
extendViews.setTextViewText(R.id.tv_no, "我是内容");
3设置悬挂式的通知栏;
private void initNotification3() {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder= new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setPriority(Notification.PRIORITY_DEFAULT);
builder.setCategory(Notification.CATEGORY_MESSAGE);
builder.setContentTitle("这是标题");
builder.setContentText("这是内容");
<strong> builder.setFullScreenIntent(pendingIntent, true);</strong>
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(NOTIFICATION_ID_NUM3,notification);
}
4通知栏分等级:
* Notification.VISIBILITY_PUBLIC:没有锁屏的时候会显示
* Notification.VISIBILITY_PRIVATE:任何情况都会显示
* Notification.VISIBILITY_SECRET:在pin password 安全锁和没有锁屏的情况才会显示。
private void initNotification4() {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,REQ_NO , intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder= new Notification.Builder(MainActivity.this);
<strong>builder.setVisibility(Notification.VISIBILITY_SECRET);</strong>
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setPriority(Notification.PRIORITY_DEFAULT);
builder.setCategory(Notification.CATEGORY_MESSAGE);
builder.setContentTitle("这是标题");
builder.setContentText("这是内容");
builder.setFullScreenIntent(pendingIntent, true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.build();
notificationManager.notify(NOTIFICATION_ID_NUM3,notification);
}