如何使用 pendingIntent Kotlin 从 Activity 传递给 BroadcastReceiver()

How to pass from Activity to BroadcastReceiver() using pendingIntent Kotlin

您好,我正在尝试使用 Wea​​therNotification activity 中的值显示来自 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

温度键上有错字,您正在接收器上初始化一个新的 Intent

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")
    //...
}