当我点击下一个或上一个按钮更改歌曲时,歌曲名称不会改变。专辑封面也不是。下面是我用于通知的服务类的代码。
public class MediaPlayerService extends Service {
Notification status;
private MainActivity main;
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
showNotification();
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Previous();
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Play();
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
Next();
} else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
Close();
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
main = new MainActivity();
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.notification_view);
RemoteViews bigViews = new RemoteViews(getPackageName(),
R.layout.notification_view);
views.setViewVisibility(R.id.status_bar_icon, View.VISIBLE);
views.setViewVisibility(R.id.iv_noti_icon, View.VISIBLE);
bigViews.setImageViewBitmap(R.id.iv_noti_icon,
Constants.getDefaultAlbumArt(this));
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Intent previousIntent = new Intent(this, MediaPlayerService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
previousIntent.putExtra("DO", "Previous");
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
previousIntent, 0);
Intent playIntent = new Intent(this, MediaPlayerService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
playIntent.putExtra("DO", "Play");
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);
Intent nextIntent = new Intent(this, MediaPlayerService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
nextIntent.putExtra("DO", "Next");
PendingIntent pnextIntent = PendingIntent.getService(this, 0,
nextIntent, 0);
Intent closeIntent = new Intent(this, MediaPlayerService.class);
closeIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
closeIntent.putExtra("DO", "Close");
PendingIntent pcloseIntent = PendingIntent.getService(this, 0,
closeIntent, 0);
views.setOnClickPendingIntent(R.id.btn_noti_play, pplayIntent);
bigViews.setOnClickPendingIntent(R.id.btn_noti_play, pplayIntent);
views.setOnClickPendingIntent(R.id.btn_noti_next, pnextIntent);
bigViews.setOnClickPendingIntent(R.id.btn_noti_next, pnextIntent);
views.setOnClickPendingIntent(R.id.btn_noti_previous, ppreviousIntent);
bigViews.setOnClickPendingIntent(R.id.btn_noti_previous, ppreviousIntent);
views.setOnClickPendingIntent(R.id.reboot, pcloseIntent);
bigViews.setOnClickPendingIntent(R.id.reboot, pcloseIntent);
views.setImageViewResource(R.id.btn_noti_play,
R.drawable.ic_pause_white);
bigViews.setImageViewResource(R.id.btn_noti_play,
R.drawable.ic_pause_white);
views.setTextViewText(R.id.tv_noti_title, "Song Title");
bigViews.setTextViewText(R.id.tv_noti_title, "Song Title");
views.setTextViewText(R.id.tv_noti_artist, "Artist Name");
bigViews.setTextViewText(R.id.tv_noti_artist, "Artist Name");
bigViews.setTextViewText(R.id.tv_noti_tag, "Album Name");
status = new Notification.Builder(this).build();
status.contentView = views;
status.flags = Notification.FLAG_ONGOING_EVENT;
status.icon = R.drawable.ic_notification;
status.contentIntent = pendingIntent;
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);
bigViews.setTextViewText(R.id.tv_noti_title, arrayList.get(next).getSongName());
bigViews.setTextViewText(R.id.tv_noti_artist, arrayList.get(next).getSingers());
bigViews.setTextViewText(R.id.tv_noti_tag, arrayList.get(next).getAlbum());
}
private void Play() {
RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.notification_view);
if (!mediaPlayer.isPlaying()) {
main.PlaySong(next, arrayList, "0");
bigViews.setImageViewResource(R.id.btn_noti_play, R.drawable.ic_pause_white);
bigViews.setTextViewText(R.id.tv_noti_title, arrayList.get(next).getSongName());
bigViews.setTextViewText(R.id.tv_noti_artist, arrayList.get(next).getSingers());
bigViews.setTextViewText(R.id.tv_noti_tag, arrayList.get(next).getAlbum());
} else {
mediaPlayer.pause();
bigViews.setImageViewResource(R.id.btn_noti_play, R.drawable.ic_play_white);
}
}
private void Next() {
RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.notification_view);
next++;
main.PlaySong(next, arrayList, "0");
bigViews.setImageViewResource(R.id.btn_noti_play, R.drawable.ic_pause_white);
bigViews.setTextViewText(R.id.tv_noti_title, arrayList.get(next).getSongName());
bigViews.setTextViewText(R.id.tv_noti_artist, arrayList.get(next).getSingers());
bigViews.setTextViewText(R.id.tv_noti_tag, arrayList.get(next).getAlbum());
}
private void Previous() {
RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.notification_view);
next--;
main.PlaySong(next,arrayList, "0");
bigViews.setImageViewResource(R.id.btn_noti_play, R.drawable.ic_pause_white);
bigViews.setTextViewText(R.id.tv_noti_title, arrayList.get(next).getSongName());
bigViews.setTextViewText(R.id.tv_noti_artist, arrayList.get(next).getSingers());
bigViews.setTextViewText(R.id.tv_noti_tag, arrayList.get(next).getAlbum());
}
private void Close() {
mediaPlayer.stop();
}
}
我在音乐应用中使用此服务类。在这个应用流播放的应用程序歌曲。喜欢" gaana" app和" saavn"应用
答案 0 :(得分:1)
致电
NotificationManager.notify(id, notification)
让Notification System知道您要更新Next()和previous()方法中的通知视图。
以下是文档链接http://developer.android.com/training/notify-user/managing.html。
希望它有所帮助。