RemoteViews.setImageViewResource не работает? - PullRequest
0 голосов
/ 25 сентября 2018

У меня есть изображение в пользовательском уведомлении. Я хочу заменить это изображение на другое после того, как оно щелкнуло, но проблема заключается в том, что после нажатия на изображение ничего не происходит

вот мой CustomNotification.xml: -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="#fa8072">


    <ImageView
        android:id="@+id/imginNotification"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:src="@drawable/defaultimg " />



</RelativeLayout

вот кнопка Показать уведомление в Mainactivity.java: - эта переменная общедоступна для всех классов lateinit var remoteViews00: RemoteViews

var btnShowNotification = findViewById<Button>(R.id.button)
    btnShowNotification.setOnClickListener {

        lateinit var  builder00:NotificationCompat.Builder
        lateinit var   notificationManager00:NotificationManager
        var  notification_id:Int = 0
        lateinit var  context00:Context


        context00 = this
        notificationManager00 = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        builder00 = NotificationCompat.Builder(this)
        remoteViews00 = RemoteViews(getPackageName(), R.layout.mycustomnoti)





        notification_id = System.currentTimeMillis().toInt()
        val button_intent = Intent("button_click")
        button_intent.putExtra("id", notification_id)
        val button_pending_event = PendingIntent.getBroadcast(context00, notification_id,
                button_intent, 0)
        remoteViews00.setOnClickPendingIntent(R.id.imginNotification, button_pending_event)
        val notification_intent = Intent(context00, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(context00, 0, notification_intent, 0)
        builder00.setSmallIcon(R.drawable.notiicon)
                .setAutoCancel(true)
                .setCustomBigContentView(remoteViews00)
                .setContentIntent(pendingIntent)
        notificationManager00.notify(notification_id, builder00.build())


    }

и вот мой BroadcastRecieverкласс: -

class changepicture:BroadcastReceiver() {
    override fun onReceive(context:Context, intent:Intent) {

    remoteViews00.setImageViewResource(R.id.imginNotification,R.drawable.musicimg)
    }
    }

и, наконец, вот приемник в AndroidManifest.xml: -

<receiver android:name=".changepicture">
    <intent-filter>
        <action android:name="button_click"/>
    </intent-filter>
</receiver>

1 Ответ

0 голосов
/ 25 сентября 2018

Обновление объекта RemoteViews недостаточно для запуска обновления уведомлений - вам нужно снова вызвать notify() с вашим сборщиком:

class changepicture:BroadcastReceiver() {
  override fun onReceive(context:Context, intent:Intent) {
    remoteViews00.setImageViewResource(R.id.imginNotification, R.drawable.musicimg)

    // Note: you need to use the exact same notification_id
    // to update the notification
    notificationManager00.notify(notification_id, builder00.build())
  }
}
...