在通知中播放/暂停button图像,Android

我已经实现了音乐播放器,在播放streamaudio时触发自定义通知

一切正常,我可以使用通知中的button播放/暂停audio。 我唯一的问题是图像button,它不能改变图像点击button指示播放/或暂停

在RemoteReciever中使用remoteViews.setImageViewResource()什么也不做

该控件是使用BroadcastReceiver完成的

这是从玩家活动中发出通知的代码

public void setNotification(String songName){ String ns = Context.NOTIFICATION_SERVICE; NotificationManager notificationManager = (NotificationManager) getSystemService(ns); @SuppressWarnings("deprecation") Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis()); RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_view); notificationView.setImageViewResource(R.id.button1, R.drawable.pause); notificationView.setTextViewText(R.id.textView1, songName); //the intent that is started when the notification is clicked (works) Intent notificationIntent = new Intent(this, PlayerActivity.class); PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); notification.contentView = notificationView; notification.contentIntent = pendingNotificationIntent; Intent switchIntent = new Intent("com.example.test.ACTION_PLAY"); PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT); notificationView.setOnClickPendingIntent(R.id.play_pause, pendingSwitchIntent); notificationManager.notify(1, notification); } 

通知xml是

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_alignParentLeft="true" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_launcher" android:id="@+id/imgAppIc" /> <TextView android:layout_toRightOf="@id/imgAppIc" android:singleLine="true" android:id="@+id/textView1" android:ellipsize="marquee" android:layout_marginLeft="7dp" android:layout_marginTop="10dp" android:marqueeRepeatLimit ="marquee_forever" android:focusable="true" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:layout_width="170dp" android:layout_height="wrap_content"/> <ImageButton android:id="@+id/play_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/play" /> </RelativeLayout> 

这是RemoteRecivier类

 public class RemoteControlReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_view); if(action.equalsIgnoreCase("com.example.test.ACTION_PLAY")){ if(mediaplayer.isPlaying()){ mediaplayer.pause(); remoteViews.setImageViewResource(R.id.button1, R.drawable.play); } else { mediaplayer.start(); remoteViews.setImageViewResource(R.id.button1, R.drawable.pause); } } } } 

最后是清单

  <receiver android:name=".RemoteControlReceiver"> <intent-filter> <action android:name="com.Music.app.ACTION_PLAY" /> </intent-filter> </receiver> <activity android:name="PlayerActivity" /> 

请帮忙解决这个,我早就被卡住了很久了

Solutions Collecting From Web of "在通知中播放/暂停button图像,Android"

您必须将notification.contentView设置为新的remoteView,以便它可以更新通知视图本身。

意思是,在您的BroadcastReceiver中收到操作后,用所需的button显示(暂停或播放)重新生成通知,

希望这可以帮助

这应该工作。 只要让你的notificationnotificationManager一个静态的全局variables。

 public class RemoteControlReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(action.equalsIgnoreCase("com.example.test.ACTION_PLAY")){ if(mediaplayer.isPlaying()){ mediaplayer.pause(); notificationView.setImageViewResource(R.id.button1, R.drawable.play); notification.contentView = notificationView; notificationManager.notify(1, notification); } else { mediaplayer.start(); notificationView.setImageViewResource(R.id.button1, R.drawable.pause); notification.contentView = notificationView; notificationManager.notify(1, notification); } } } } 

为什么不使用ImageView而不是ImageButton? 使用ImageView,您可以简单地使用removeViews.setImageViewResource(R.id.button1, R.drawable.pause);更改其外观removeViews.setImageViewResource(R.id.button1, R.drawable.pause);