android getSystemService需要NOTIFICATION_SERVICE作为Class< TypeVariable(T)!>

holgip5t  于 2023-06-27  发布在  Android
关注(0)|答案(2)|浏览(151)

所以我想为我的应用程序创建一个通知提醒。但是当我想创建一个通知通道时,我已经在互联网和文档中查看了代码,但是getSystemService方法显示了一个错误。

  1. Type mismatch: inferred type is String but Class<TypeVariable(T)!> was expected

我的准则

  1. private fun createNotificationChannel()
  2. {
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
  4. val name = "Notif Channel"
  5. val desc = "A Description of the Channel"
  6. val importance = NotificationManager.IMPORTANCE_DEFAULT
  7. val channel = NotificationChannel(channelID, name, importance)
  8. channel.description = desc
  9. //line below error
  10. val notificationManager = getSystemService(requireContext(), NOTIFICATION_SERVICE) as NotificationManager
  11. //----------------
  12. notificationManager.createNotificationChannel(channel)
  13. }
  14. }

有人知道怎么处理这个吗?

7kjnsjlb

7kjnsjlb1#

正确的用法是:

  1. val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

  1. val notificationManager = getSystemService(requireContext(), NotificationManager::class.java)
66bbxpm5

66bbxpm52#

另一种使用方法是:

  1. val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

相关问题