Android通知栏学习(基础篇)
16lz
2021-01-25
Android通知栏学习
基础方法
步骤一:NotificationManager
状态栏通知的管理类,负责发通知、清除通知等操作
//实例化方法 NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
几个方法:
// 清除通知,就是把存在通知栏的通知去掉cancel(int id)cancel(String tag, int id)cancelAll()// 在状态栏显示一个通知notify(int id, Notification notification)notify(String tag, int id, Notification notification)
步骤二:Notification.Builder
通知构造类,主要方便对通知属性的各种设置(API level 4)
实例化方法
Notification.Builder mBuilder = new new Notification.Builder(mContext);
常用方法如下:
// 设置通知标题 // Set the first line of text in the platform notification template. setContentTitle(CharSequence title) // 设置通知的文字 // Set the second line of text in the platform notification template. setContentText(CharSequence text) // 设置通知提示信息,就是状态栏提示时到文字 // Set the "ticker" text which is displayed in the status bar when the notification // first arrives. setTicker(CharSequence tickerText) // 设置通知的图片 // Set the small icon resource, which will be used to represent the notification in // the status bar. setSmallIcon(int icon) // 用户点击通知栏时自动退出通知栏,个人经验需要与setContentIntent (PendingIntent intent) // 配合使用才能看到效果 // Make this notification automatically dismissed when the user touches it. setAutoCancel (boolean autoCancel) // 可以用来设置点击跳转意图 // Supply a PendingIntent to be sent when the notification is clicked. setContentIntent (PendingIntent intent) // 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间 // Add a timestamp pertaining to the notification (usually the time // the event occurred). setWhen (long when)
特殊用法
// 当用户取消通知栏时,此意图有效setDeleteIntent (PendingIntent intent)
步骤三:Notification
这个就是通知类了
最简单的实例化方法
Notification mNotification = mBuilder.build();
步骤四:最后一步,发送通知请求
直接一句代码搞定
mNotificationManager.notify(notifyId, mBuilder.build()); //或者mNotificationManager.notify(notifyTag,notifyId, mBuilder.build());
就是NotificationManager的方法,在上面又提到。
更多相关文章
- Android学习笔记之获得屏幕属性DisplayMetrics
- Android(安卓)build文件的删除的方法
- 正确的Flutter和Android交互代码
- android添加以太网ethernet方法 android框架添加
- MediaScanner源码分析
- Android(安卓)Camera预览左右上下镜像
- Fragment与Activity之间数据交互(Kotlin)
- Android(安卓)中Intent的putExtra()方法的值传递
- android微信朋友圈分享