Android 通知用法
1、首先创建一个NotifyManager对通知进行管理,调用Context的 getSystemService() 方法。getSystemService() 方法用于接收一个字符串来确定从系统获取什么服务:Context.NOTIFICATION_SERVICE 代表获取系统的通知服务。
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {var channel =NotificationChannel("normal", "Normal", NotificationManager.IMPORTANCE_DEFAULT)manager.createNotificationChannel(channel)
}
2、使用 NotificationChannel 类创建实例,传入的参数为:
channelId:代表这个通知的渠道ID 具有唯一性
channelName:该通知渠道名称
通知的重要等级:重要等级主要有IMPORTANCE_HIGH、IMPORTANCE_DEFAULT、
IMPORTANCE_LOW、IMPORTANCE_MIN这几种
由于NotificationChannel类和createNotificationChannel()方法都是Android 8.0系统中新增的API,因此我们在使用的时候还需要进行版本判断才可以!!!!
3、使用 NotificationCompat.Builder 来构建通知对象。
NotificationCompat.Builder 中接收两个参数:第一个参数是context,这个没什么好说的;第二个参数是渠道ID。还提供了对通知设置内容的多种方法,其它的方法功能如名称所示!!
sendNotice.setOnClickListener {val notification =NotificationCompat.Builder(this, "normal").setContentTitle("This is content title").setContentText("This is content text!").setSmallIcon(R.drawable.small_icon).setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.large_icon)).setContentIntent(pendingIntent).build()manager.notify(1, notification)
4、设置点击通知功能
使用 PendingIntent ,PendingIntent提供了很多方法:getActivity()方法、getBroadcast()方法,还是getService()方法,这几个方法的参数相同:
第一个:Context
第二个:参数一般用不到,传入0即可
第三个是一个Intent对象,用于构建PendingIntent的“意图”
第四个用于确定PendingIntent的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT这4种值可选
sendNotice.setOnClickListener {val intent = Intent(this, NotificationActivity::class.java)val pendingIntent = PendingIntent.getActivity(this, 0, intent, FLAG_MUTABLE)val notification =NotificationCompat.Builder(this, "normal").setContentTitle("This is content title").setContentText("This is content text!").setSmallIcon(R.drawable.small_icon).setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.large_icon)).setContentIntent(pendingIntent).setAutoCancel(true).build()manager.notify(1, notification)}
setAutoCancel 为设置当点击通知后,通知自动取消的功能。
5、进阶技巧
NotificationCompat.Builder 中提供了非常丰富的API,以便我们创建出更加多样的通知效果!!
(1) setStyle() 用于通知显示 长文本、大图片等功能的实现。实现代码:
长文本:
.setStyle(
NotificationCompat.BigTextStyle().bigText(
"This is content text!This is content text!This is content text!" +
"This is content text!This is content text!This is content text!" +
"This is content text!This is content text!This is content text!")
)
.build()
大图片:
.setStyle(NotificationCompat.BigPictureStyle().bigPicture(
BitmapFactory.decodeResource(resources, R.drawable.big_image)))
Android 通知用法相关推荐
- xamarin android 通知,在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- Android通知,PendingIntent示例
Welcome to Android Notification Example using android PendingIntent. In this tutorial we're going to ...
- Android通知怎么实现?Android开发如何操作相机和相册?
Android通知怎么实现?Android开发如何操作相机和相册? 前言 八.Android通知怎么实现?Android开发如何操作相机和相册? 8.1 通知介绍 8.2 通知的基本用法 8.3 An ...
- 【转】 Android - LayoutInflate用法
[转自]http://blog.csdn.net/scut1135/article/details/7055461 通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Acti ...
- 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
场景:实现安装一个apk应用程序的过程.界面如下: 编写如下应用,应用结构如下: <RelativeLayout xmlns:android="http://schemas.an ...
- android activity 被notification启动,Android通知Notification全面剖析
原标题:Android通知Notification全面剖析 通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通 ...
- MTK驱动(77)---Android getevent用法
Android getevent用法 getevent命令用法如下: Usage: getevent [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [ ...
- Android通知频道,通知点
In this tutorial, we'll be looking into how the introduction of Android Oreo has brought a drastic c ...
- android通知悬浮通知_Android通知直接回覆
android通知悬浮通知 Android Notification Direct Reply action lets us reply to the notification message, it ...
- android 通知静音_如何使电话静音(但不包括短信和通知)
android 通知静音 If you don't want to hear your phone ring, but do want to hear text messages and other ...
最新文章
- fragment之间通信
- 单机安装oracle,Oracle 11G 单机安装
- 最小生成树 次小生成树
- Huggingface简介及BERT tansformer 开源
- python爬携程酒店评论_python爬虫爬取携程网的酒店评论数据时,有个请求参数不知道是怎么生成的?...
- java8 stream遍历_Java8中用法优雅的 Stream,性能也优雅吗?
- java基础巩固_(一)Java基础巩固
- c 语言输入n个数求和,c++---天梯赛---N个数求和
- linux创建tcp命令是,linux – 为tcpdump捕获创建多播联接
- android开发_SimpleAdapter适配器
- git revert 回滚代码至上一版本
- (第一天)Oracle数据库学习
- 泰然金融全国用户见面会走进豫陕,与用户零距离对话
- 如何去掉桌面图标快捷方式的小箭头(小技巧)
- iOS APP打开微信小程序
- TensorFlow 卷积神经网络之猫狗识别
- BufferedRead
- html5简单幻灯片图片转换,用纯CSS实现简单的相册幻灯片
- 拓扑排序在实际项目中应用
- 苏宁易购工作怎么样_福建苏宁总经理感恩节家访 给老会员送免费清洗家电福利...