官网参考:
https://developer.android.google.cn/media/implement/playback-app?hl=zh-cn#implementing_a_mediasessionservice
https://developer.android.google.cn/develop/ui/views/notifications/expanded?hl=zh-cn
AndroidManifest.xml
<service android:name=".service.MediaPlayerService"
android:foregroundServiceType="mediaPlayback"></service> //如果没有notification点击跳转,就不需要这句
Service中:
注意:增加builder.setCustomBigContentView(mNotificationView) !
去除.setSmallIcon(R.drawable.ic_notification_recording)!
private void setNotification () {
if (mItem == null) return;
mNotificationView = new RemoteViews(getPackageName(), R.layout.notification);
mNotificationView.setTextViewText(R.id.app_name, getResources().getString(R.string.app_name));
mNotificationView.setTextViewText(R.id.file_name, mItem.getFileName());
String action = null;
if (mIntent != null) action = mIntent.getAction();
changeNotification(action);
if (action.equals(ACTION_STOP)) return;
Intent intent = null;
PendingIntent pendingIntent = null;
intent = new Intent(this, MediaPlayerService.class);
intent.setAction(ACTION_GOON_PLAY);
pendingIntent = PendingIntent.getService(this, 0, intent, FLAG_MUTABLE);
mNotificationView.setOnClickPendingIntent(R.id.btn_goon_play, pendingIntent);
intent = new Intent(this, MediaPlayerService.class);
intent.setAction(ACTION_PAUSE);
pendingIntent = PendingIntent.getService(this, 0, intent, FLAG_MUTABLE);
mNotificationView.setOnClickPendingIntent(R.id.btn_pause, pendingIntent);
intent = new Intent(this, MediaPlayerService.class);
intent.setAction(ACTION_STOP);
pendingIntent = PendingIntent.getService(this, 0, intent, FLAG_MUTABLE);
mNotificationView.setOnClickPendingIntent(R.id.btn_stop, pendingIntent);
Bundle bundle = new Bundle();
bundle.putSerializable("ITEM", mItem);
intent = new Intent(this, MediaActivity.class);
intent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Notification notification = new Notification();
notification.contentView = mNotificationView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.icon = R.drawable.ic_notification_recording;
notification.contentIntent = pendingIntent;
setChannel();
Notification.Builder builder = new Notification.Builder(mContext, notification)
.setSmallIcon(R.drawable.ic_notification_recording)
.setChannelId(channelId)
.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
notification = builder.build();
startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
/*mBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification_recording)
.setCustomContentView(mNotificationView);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel (channelId, getResources().getString(R.string.app_name),
NotificationManager.IMPORTANCE_LOW);
mNotificationManager.createNotificationChannel(channel);
Notification notification = mBuilder
.setContentIntent(pendingIntent).build();
mNotificationManager.notify(notificationID, notification);*/
}
private void setChannel () {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel (channelId, getResources().getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
if (channel == null) {
channel = new NotificationChannel(channelId,
getResources().getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(false);
channel.enableLights(false);
channel.enableVibration(false);
channel.setBypassDnd(true);
channel.setSound(null,null);
mNotificationManager.createNotificationChannel(channel);
}
}
private void cancelNotification () {
mItem = null;
if (mNotificationManager != null) {
/*mNotificationManager.cancel(notificationID);*/
stopForeground(true);
mNotificationManager = null;
}
}
private void changeNotification (String action) {
if (action == null) return;
if (action.equals(ACTION_GOON_PLAY)) {
mNotificationView.setViewVisibility(R.id.btn_pause, View.VISIBLE);
mNotificationView.setViewVisibility(R.id.btn_goon_play,View.GONE);
mNotificationView.setViewVisibility(R.id.btn_stop,View.VISIBLE);
if (mMediaPlayerUtil != null && mMediaPlayerUtil.getmPlayerState() != MediaState.PLAYING) {
mMediaPlayerUtil.mediaPlayerStart();
}
} else if (action.equals(ACTION_PAUSE)) {
mNotificationView.setViewVisibility(R.id.btn_pause,View.GONE);
mNotificationView.setViewVisibility(R.id.btn_goon_play,View.VISIBLE);
mNotificationView.setViewVisibility(R.id.btn_stop,View.VISIBLE);
if (mMediaPlayerUtil != null && mMediaPlayerUtil.getmPlayerState() != MediaState.PAUSE) mMediaPlayerUtil.mediaPlayerPause();
} else if (action.equals(ACTION_STOP)) {
cancelNotification();
if (mMediaPlayerUtil != null && mMediaPlayerUtil.getmPlayerState() == MediaState.PLAYING) {
mMediaPlayerUtil.mediaPlayerStop();
}
} else if (action.equals(ACTION_RESTART)) {
mNotificationView.setViewVisibility(R.id.btn_pause, View.VISIBLE);
mNotificationView.setViewVisibility(R.id.btn_goon_play,View.GONE);
mNotificationView.setViewVisibility(R.id.btn_stop,View.VISIBLE);
}
}