使用芬兰湾的科特林怎么实现一个通知栏提醒功能

  介绍

本篇文章为大家展示了使用芬兰湾的科特林怎么实现一个通知栏提醒功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1,创建一个空活动项目后,编辑activity_main。xml文件

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   LinearLayout & lt;   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:取向=皏ertical"   ,xmlns: android=癶ttp://schemas.android.com/apk/res/android"比;   & lt;按钮   android才能:onClick=皊howNotification"   android:文本=安拍芟允就ㄖ?   android:才能layout_width=癿atch_parent"   android:才能layout_height=皐rap_content"/比;   & lt;/LinearLayout>

2,在类,MainActivity 创建,showNotification方法

fun  showNotification(视图:视图)   {   ,//CHANNEL_ID:通道ID,可在类,MainActivity 外自定义。如:val  CHANNEL_ID =, & # 39; msg_1& # 39;   ,val  builder =, NotificationCompat.Builder(这个,,CHANNEL_ID)   .setSmallIcon才能(R.mipmap.ic_launcher)   .setContentTitle才能(“RNG赛程提醒“)   .setContentText才能(“今天晚上晚7:00,提高对阵IG")//,才能通知优先级,可以设置为int型,范围2至2   .setPriority才能(NotificationCompat.PRIORITY_MAX )   ,//显示通知   ,(NotificationManagerCompat.from(这),{   通知才能(1,builder.build ())   ,}   }

3,为了兼容Android 8.0及更高版本,传递通知之前,必须在系统中注册应用程序的通知通道。创建好后在> private  fun  createNotificationChannel ()   {   ,if  (Build.VERSION.SDK_INT 祝辞=,Build.VERSION_CODES.O), {   val 才能;name =, getString (R.string.channel_name)   val 才能;descriptionText =, getString (R.string.channel_description)//,才能提醒式通知(横幅显示),不过大部分需要手动授权   val 才能;importance =NotificationManager.IMPORTANCE_HIGH   val 才能;channel =, NotificationChannel (CHANNEL_ID、,名称,重要性).apply  {description =, descriptionText}//才能,注册通道(频道)   val 才能notificationManager: NotificationManager =, getSystemService (Context.NOTIFICATION_SERVICE), as  notificationManager   notificationManager.createNotificationChannel才能(频道)   ,}   }

上述内容就是使用芬兰湾的科特林怎么实现一个通知栏提醒功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

使用芬兰湾的科特林怎么实现一个通知栏提醒功能