Android 实现不同通知栏

这篇博客详细介绍了如何在Android中实现不同类型的 notification,包括普通通知栏、自定义布局通知栏和悬浮式通知。通过获取通知管理器实例,开发者可以创建并设置通知的可见性,如 `VISIBILITY_PUBLIC`、`VISIBILITY_PRIVATE` 和 `VISIBILITY_SECRET`,在Android 5.0及以上版本还可以使用 `builder.setVisibility()` 进行控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

一,普通通知栏:

这里写图片描述

代码如下:

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());

二,自定义布局通知栏:

这里写图片描述

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

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);

三,悬挂式的通知:

这里写图片描述
代码如下:(布局就是一个基本的布局)

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);设置.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值