Android初步进阶之通知Notification

我了解到的通知,可以有三种展示类型,分别为普通通知、悬挂通知和折叠通知。而我们可以常用自定义视图、触发事件和权限等对它进行设置,个性化。

下面介绍一个普通的通知的构建。
示例代码:

  1. 普通通知
//触发事件
            val mIntent:Intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"))
            //通知管理器
            val manager:NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            //通知触发事件Intent
            val pendingIntent:PendingIntent = PendingIntent.getActivity(this, 0 ,mIntent, 0)
            //信道创建和设置,注意channelId
            val notificationChannel:NotificationChannel = NotificationChannel("test", "测试普通", NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(notificationChannel)
            //构建者模式创建通知
            val builder:Notification = NotificationCompat.Builder(this@MainActivity, "test")
                .setAutoCancel(true)
                .setContentTitle("普通通知")
                .setContentIntent(pendingIntent)
                .setContentText("测试")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_background))
                .build()
                //发送通知
            manager.notify(1, builder)

  1. 悬挂通知
    使用setFullScreenIntent来将notification变为悬挂式。
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
  1. 折叠通知
    使用RemoteViews来创建折叠的视图对象,设置notification.contentView为remoteViews,或者notification.bigContentView为remoteViews。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值