我正在使用Notification类发布用于自定义媒体播放器控件的解决方案。我希望它可以帮助其他人并节省他们的时间。
新的androidx对媒体播放器对象具有特殊的风格,我在他们的文档中没有看到它,但是效果很好:
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle())
确保同时提供了大图标和小图标。
按钮添加有“ addAction”。此方法需要一个未决的意图。这是作为广播创建的,但保留在应用程序上下文中。在这种情况下,我已经注册了两个BroadcastRecivers:一个用于“播放”按钮,另一个用于“暂停”按钮。可以先做一个,然后再切换/案例。
最后,必须创建一个通知频道。没有该代码,根本不会显示任何通知。
public class PlayBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Play!! Starting the media player here.", Toast.LENGTH_LONG).show();
}
}
public class StopBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Pause!! Pausing the media player here.", Toast.LENGTH_LONG).show();
}
}
void initPlayerControls() {
Intent notifyIntent = new Intent(this, MainActivity.class);
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
final String PLAY = "tech.mycompany.app.PLAY";
Intent playIntent = new Intent(PLAY);
PendingIntent playPendingIntent =
PendingIntent.getBroadcast(getBaseContext(), 0, playIntent, 0);
IntentFilter playIntentFilter = new IntentFilter();
playIntentFilter.addAction(PLAY);
getBaseContext().registerReceiver(new PlayBroadcastReceiver(),playIntentFilter);
final String STOP = "tech.mycompany.app.STOP";
Intent stopIntent = new Intent(STOP);
PendingIntent stopPendingIntent =
PendingIntent.getBroadcast(getBaseContext(), 0, stopIntent, 0);
IntentFilter stopIntentFilter = new IntentFilter();
stopIntentFilter.addAction(STOP);
getBaseContext().registerReceiver(new StopBroadcastReceiver(),stopIntentFilter);
final Notification notification = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.top_logo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.top_logo))
.addAction(R.drawable.icon_play, "Play", playPendingIntent) // #1
.addAction(R.drawable.icon_pause, "Pause", stopPendingIntent) // #2
.setContentTitle("Wonderful music")
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle())
.setContentIntent(notifyPendingIntent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.app_medium_color_transparent))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CH, "player", importance);
channel.setDescription("Player channel");
NotificationManager nm = getSystemService(NotificationManager.class);
if (null != nm) {
nm.createNotificationChannel(channel);
}
}
// notificationId is a unique int for each notification that you must define
notificationManager.notify(888, notification);
}