RemoteViews.setImageViewResource 不起作用?
RemoteViews.setImageViewResource doesn't work?
我在自定义通知中有一个 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 中的显示通知按钮:-
此 var 对所有 classes lateinit var remoteViews00:RemoteViews
是 public
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 :-
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>
更新 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())
}
}
我在自定义通知中有一个 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 中的显示通知按钮:- 此 var 对所有 classes lateinit var remoteViews00:RemoteViews
是 publicvar 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 :-
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>
更新 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())
}
}