Добавить действие swtich (Play / Pause) к уведомлению - PullRequest
0 голосов
/ 09 января 2019

Мое приложение воспроизводит музыку с помощью уведомлений медиа-контроллера ( PLAY и PAUSE ), мой код ниже отображает два действия PLAY и PAUSE, как я могу сделать один button для паузы и играть музыку вместо двух.

  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());

    }

Я хочу отобразить уведомление одной кнопкой ( PLAY ), когда пользователь щелкает по нему, поворачивается к (ПАУЗА) (как кнопка toggle)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...