【问题标题】:How to pass from Activity to BroadcastReceiver() using pendingIntent Kotlin [closed]如何使用 pendingIntent Kotlin 从 Activity 传递到 BroadcastReceiver() [关闭]
【发布时间】:2020-10-08 09:13:50
【问题描述】:

您好,我正在尝试使用 Wea​​therNotification 活动中的值显示来自 WeatherBroadcater BroadcastReceiver 的温度值和天气描述通知。但是,我无法检索这些值,因为它在 BroadcastReceiver 上显示为 null。检查我该怎么做,并提前感谢

WeatherNotification.kt

private fun setIntent() {
    val intent = Intent(this, WeatherBroadcaster::class.java)

    Log.d("Broadcast", "sending to broadcast Temp: ${weatherResult.temp}, Desc: ${weatherResult.weatherDescription}")
    intent.putExtra("Temp", weatherResult.temp.toString())
    intent.putExtra("WeatherDes", weatherResult.weatherDescription.toString())
    val pendingIntent: PendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

    val timeStart = System.currentTimeMillis()
    val interval = 1000

    alarmManager.setRepeating(
        AlarmManager.RTC_WAKEUP,
        timeStart,interval.toLong(), pendingIntent
    )
}

WeatherBroadcaster.kt

    override fun onReceive(context: Context?, intent: Intent?) {
    val intent = Intent(context, WeatherNotification::class.java)

    val temp: String? = intent.getStringExtra("Temp")
    val desc: String? = intent.getStringExtra("WeatherDes")
    Log.d("Broadcast", "sending to broadcast Temp: $temp, Desc: $desc")

    val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

    var bitmap = BitmapFactory.decodeResource(context?.resources, R.drawable.pressure)
    var bitmapSun =
        BitmapFactory.decodeResource(context?.resources, R.drawable.sunrise)

    val builder = NotificationCompat.Builder(context!!, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle(temp)
        .setContentText(desc)
        .setLargeIcon(bitmapSun)
        .setStyle(NotificationCompat.BigPictureStyle().bigPicture(bitmap))
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

    with(NotificationManagerCompat.from(context)) {
        notify(notificationId, builder.build())
    }
}

LogCat

2020-10-08 16:56:13.774 6462-18591/com.sgtech.ict3104.racecar D/Broadcast: sending to broadcast Temp: 30.97°C, Desc: light rain

2020-10-08 16:56:33.227 6462-6462/com.sgtech.ict3104.racecar D/Broadcast: sending to broadcast Temp: null, Desc: null

【问题讨论】:

  • "temp""Temp" 不同

标签: android kotlin broadcastreceiver android-notifications android-pendingintent


【解决方案1】:

温度键上有错字,您正在初始化接收器的新意图

override fun onReceive(context: Context?, intent: Intent?) {
    val intent = Intent(context, WeatherNotification::class.java)
    //...
}

val intent 是您从方法签名上的 context 参数创建的新 Intent。您需要改为检查 intent 参数。

override fun onReceive(context: Context?, intent: Intent?) {
    //the intent is now the same as in the argument, the same received
    intent ?: return
    val temp: String? = intent.getStringExtra("temp")
    val desc: String? = intent.getStringExtra("WeatherDes")
    Log.d("Broadcast", "sending to broadcast Temp: $temp, Desc: $desc")
    //...
}

【讨论】:

  • 非常感谢您的帮助!如果我要将数据从接收器传递到另一个活动并在通知上显示我的温度,我可以与您核对,我是否在执行“getStringExtra”后初始化了一个新意图?再次感谢!
  • 是的,你需要一个新的意图,因为以前的意图没有目的地,记住意图构造函数可以接收 2 个参数,上下文和目标类。所以为新活动创建一个新活动并转发附加内容,好在你不需要一个一个得到它们,看看这个stackoverflow.com/a/13686256/4017501顺便说一句你也可以投票给答案,谢谢你把它标记为解决方案
  • 非常感谢您的详细解释以及链接。不幸的是,尽管我很想为这个答案投票,但我现在的声誉目前为 2,因为我今天才创建了一个 stackoverflow 帐户。再次非常感谢,希望你有一个美好的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
相关资源
最近更新 更多