我在自定义通知中有一个Imageview,我想将此imageview单击后替换为另一个 但是问题是单击Imageview后没有任何反应
这是我的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>
答案 0 :(得分:0)
更新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())
}
}