Android 实现不同通知栏

第一步:(不管哪种方式都要先得到通知管理器的实例)

 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

一,普通通知栏:

Android 实现不同通知栏_第1张图片

代码如下:

Notification.Builder builder=new Notification.Builder(this);
          Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
          PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);


          builder.setContentIntent(pendingIntent);
          builder.setSmallIcon(R.mipmap.ic_launcher);
          builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
          builder.setAutoCancel(true);
          builder.setContentTitle("普通通知栏");
          mNotificationManager.notify(1, builder.build());

二,自定义布局通知栏:

Android 实现不同通知栏_第2张图片

代码如下:(布局就是一个基本的布局)

notification.bigContentView=remoteViews;
notification.contentView=remoteViews;
Notification.Builder builder2=new Notification.Builder(this);
          Intent intent2=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
          PendingIntent pendingIntent2=PendingIntent.getActivity(this,0,intent2,0);
          builder2.setContentIntent(pendingIntent2);
          builder2.setSmallIcon(R.mipmap.ic_launcher);
          builder2.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
          builder2.setAutoCancel(true);
          builder2.setContentTitle("折叠通知");

          RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_view);
          Notification  notification=builder2.build();
          notification.bigContentView=remoteViews;
          mNotificationManager.notify(1,notification);

三,悬挂式的通知:

Android 实现不同通知栏_第3张图片
代码如下:(布局就是一个基本的布局)

Notification.Builder builder3=new Notification.Builder(this);
          Intent intent3=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
          PendingIntent pendingIntent3=PendingIntent.getActivity(this,0,intent3,0);
          builder3.setContentIntent(pendingIntent3);
          builder3.setSmallIcon(R.mipmap.ic_launcher);
          builder3.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
          builder3.setAutoCancel(true);
          builder3.setContentTitle("悬挂通知栏");

          Intent XuanIntent=new Intent();
          XuanIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          XuanIntent.setClass(this,MainActivity.class);

          PendingIntent xuanpengdIntent=PendingIntent.getActivity(this,0,XuanIntent,PendingIntent.FLAG_CANCEL_CURRENT);
          builder3.setFullScreenIntent(xuanpengdIntent,true);
          mNotificationManager.notify(2,builder3.build());

另外:
VISIBILITY_PUBLIC: 任何情况的显示
VISIBILITY_PRIVATE: 只有在没有锁屏时显示
VISIBILITY_SECRET: 在安全锁下或者没锁屏下显示
Android5.0以后可以通过builder.setVisibility(Notification.VISIBILITY_PUBLIC);设置.

你可能感兴趣的:(Android通知栏)