我有以下代码,可以在用户开始播放音乐时创建通知;
private Notification buildNotification(String title, String artist, Bitmap art) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder.setOngoing(true);
notificationBuilder.setAutoCancel(true); //TODO revert this to false depending on effect
notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1,
new Intent(getApplicationContext(), MusicPlayer.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);
Intent previousTrackIntent = new Intent(getApplicationContext(), MusicService.class);
previousTrackIntent.setAction(MusicService.ACTION_REWIND);
PendingIntent previousTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, previousTrackIntent, 0);
Intent playPauseTrackIntent = new Intent(getApplicationContext(), MusicService.class);
playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
PendingIntent playPauseTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, playPauseTrackIntent, 0);
Intent nextTrackIntent = new Intent(getApplicationContext(), MusicService.class);
nextTrackIntent.setAction(MusicService.ACTION_SKIP);
PendingIntent nextTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, nextTrackIntent, 0);
Intent stopServiceIntent = new Intent(getApplicationContext(), MusicService.class);
stopServiceIntent.setAction(MusicService.ACTION_STOP);
PendingIntent stopServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, stopServiceIntent, 0);
if (isPlayingMusic()) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
} else {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
}
compactNotification.setTextViewText(R.id.notification_base_line_one, title);
compactNotification.setTextViewText(R.id.notification_base_line_two, artist);
expandedNotification.setTextViewText(R.id.notification_expanded_base_line_one, title);
expandedNotification.setTextViewText(R.id.notification_expanded_base_line_two, artist);
compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_play, playPauseTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_next, nextTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_previous, previousTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);
expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, art);
compactNotification.setImageViewBitmap(R.id.notification_base_image, art);
notificationBuilder.setContent(compactNotification);
Notification notification = notificationBuilder.build();
if (Build.VERSION.SDK_INT >= 16)
notification.bigContentView = expandedNotification;
notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_ONGOING_EVENT;
return notification;
}
会显示通知,但有一些问题:
应该切换按钮的代码是:
public boolean isPlayingMusic() {
try {
return mPlayer.isPlaying();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
停止播放时应取消通知的代码是:
private void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}