Android 实现不同通知栏
16lz
2021-01-23
第一步:(不管哪种方式都要先得到通知管理器的实例)
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);设置.
更多相关文章
- Android 显示当前服务的代码片段
- Android 布局讲解
- android 开发,用一行代码操作只使用一次的 view
- android studio 61歌曲服务器搭建 歌曲app 下载 完整代码
- Android 仿抖音上下滑动布局
- [转]代码实现渐变背景
- Android UI布局经验总结
- Android左右滑动实现Activity切换类 (整合代码实例)
- 获取Android各类系统相关信息的接口实现代码
随机推荐
- Android(安卓)Studio使用小结
- Android(安卓)Service启动(二) bindService
- SmartFoxServer: massive multiplayer ga
- Android之设置页面(PreferenceActivity使
- Dojo mobile TweetView 系列教程之五 —
- 2011.09.26(2)——— android sample之Note
- android 常用的数据库表以及操作说明
- 4.11笔记 android database打开方式,ios反
- android用户界面-组件Widget-画廊视图Gal
- Android动态显示隐藏密码输入框的内容