我已经实现了音乐播放器,在播放流音频时会触发自定义通知.
一切正常,我可以使用通知中的按钮播放/暂停音频.唯一的问题是图像按钮:单击按钮无法更改图像以指示播放/暂停.
在RemoteReceiver中使用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); …Run Code Online (Sandbox Code Playgroud) 如何从通知中的按钮控制音乐播放器,以及如何从通知中收听按钮操作
notifications android audio-player android-music-player remoteview