将通知动作(播放/暂停)添加到通知中

时间:2019-01-09 20:01:57

标签: android android-mediaplayer android-notifications

我的应用通过通知媒体控制器( PLAY PAUSE )操作播放音乐,下面的代码显示了PLAY和PAUSE两个操作,我如何制作一个{{1 }}暂停播放音乐,而不是两个。

button

我想在用户单击时用一个按钮( PLAY )显示通知,然后转到(PAUSE)(类似于 public void createNotification(Context context, String all, String channelId, NotificationManager notificationManager) { Intent yesReceive = new Intent(NOTIFICATION_SERVICE); yesReceive.setAction(MyIntentSerice.RESUME_ACTION); PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, MyIntentSerice.REQUEST_CODE_NOTIFICATION, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action actionplay = new NotificationCompat.Action.Builder(R.drawable.playicon, "Play", pendingIntentYes).build(); Intent yesReceive2 = new Intent(NOTIFICATION_SERVICE); yesReceive2.setAction(MyIntentSerice.STOP_ACTION); PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, MyIntentSerice.REQUEST_CODE_NOTIFICATION, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action actionpause = new NotificationCompat.Action.Builder(R.drawable.pauseicon, "Pause", pendingIntentYes2).build(); NotificationCompat.Builder b = new NotificationCompat.Builder(context, channelId); Intent myIntent = new Intent(context, MainActivity.class); myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myIntent, 0); b.setAutoCancel(false); b.setDefaults(Notification.DEFAULT_ALL); b.setWhen(System.currentTimeMillis()); Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon2); b.setContentTitle(title); b.setContentText(artist); b.setContentIntent(contentIntent); b.setDefaults(Notification.DEFAULT_LIGHTS); b.setPriority(Notification.PRIORITY_MAX); b.setShowWhen(true); b.setColor(ContextCompat.getColor(context, R.color.khdar)); b.addAction(actionpause); b.addAction(actionplay); notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); assert notificationManager != null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "app name", NotificationManager.IMPORTANCE_LOW); notificationManager.createNotificationChannel(channel); channel.setDescription("no sound"); channel.setSound(null, null); channel.enableLights(false); channel.setLightColor(Color.RED); channel.enableVibration(false); } if (Build.VERSION.SDK_INT < 16) { notificationManager.notify(1, b.getNotification()); } else notificationManager.notify(1, b.build()); } 按钮)

0 个答案:

没有答案