欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

kotlin實現(xiàn)通知欄提醒功能示例代碼

 更新時間:2019年08月11日 11:18:10   作者:男兒當自強  
這篇文章主要給大家介紹了關于kotlin實現(xiàn)通知欄提醒功能的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用kotlin具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

一、概述

2019年英雄聯(lián)盟LPL賽區(qū)賽季賽打得火熱,作為一個RNG粉絲,想通過app實現(xiàn)RNG賽程提醒,于是就有了這次技術實踐。我在網(wǎng)上找了很久,幾乎沒找到使用kotlin實現(xiàn)通知欄提醒的合適的文章,于是就到安卓官網(wǎng)看文檔,一邊翻譯一邊研究,最終實現(xiàn)了一個簡單的通知欄提醒。又研究了定時任務,但沒有成功,還望看到的大佬給個錦囊。

二、環(huán)境

kotlin版本:1.3.31

android studio版本:3.4.1

在華為手機android 9 API28 環(huán)境下測試通過

三、實現(xiàn)

1、創(chuàng)建一個 Empty Activity 項目后,編輯 activity_main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <Button
  android:onClick="showNotification"
  android:text="顯示通知"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>
</LinearLayout>

2、在類  MainActivity 創(chuàng)建 showNotification 方法

fun showNotification(view: View)
{
 // CHANNEL_ID:通道ID,可在類 MainActivity 外自定義。如:val CHANNEL_ID = 'msg_1'
 val builder = NotificationCompat.Builder(this, CHANNEL_ID)
  .setSmallIcon(R.mipmap.ic_launcher)
  .setContentTitle("RNG賽程提醒")
  .setContentText("今天晚上19:00,RNG對陣IG")
  // 通知優(yōu)先級,可以設置為int型,范圍-2至2
  .setPriority(NotificationCompat.PRIORITY_MAX )
 // 顯示通知
 with(NotificationManagerCompat.from(this)) {
  notify(1, builder.build())
 }
}

3、為了兼容Android 8.0及更高版本,傳遞通知之前,必須在系統(tǒng)中注冊應用程序的通知通道。創(chuàng)建好后在 onCreate 函數(shù)內(nèi)調用

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, name, importance).apply {description = descriptionText}
  // 注冊通道(頻道)
  val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  notificationManager.createNotificationChannel(channel)
 }
}

四、總結

對于報錯部分,可以使用 Alt+Enter 組合鍵完成錯誤更正。

詳細的通知使用,請轉到官網(wǎng)研究。developer.android.google.cn/training/no

初次發(fā)文,若有不足的地方,還請指正。成品截圖

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

相關文章

最新評論