通知不会显示所有操作按钮

时间:2018-06-11 13:23:20

标签: java android notifications

我试图为有3个按钮的音乐播放器创建一个简单的通知,暂停,上一个和下一个。我的代码只显示其中一个是Next。我做错了什么建议? It only shows one action button

Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Intent playNextIntent = new Intent(this, BroadcastReceiver.class);
    playNextIntent.setAction("NEXT_ACTION");
    PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 1, playNextIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent pauseIntent = new Intent(this, BroadcastReceiver.class);
    pauseIntent.setAction("PAUSE_ACTION");
    PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 2, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent playPreviousIntent = new Intent(this, BroadcastReceiver.class);
    playPreviousIntent.setAction("PREVIOUS_ACTION");
    PendingIntent prevPendingIntent = PendingIntent.getBroadcast(this, 3, playPreviousIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "D")
            .setSmallIcon(R.drawable.ic_launcher_background) //notification icon
            .setContentTitle("Simple Music Player")
            .setContentText("Currently playing: " + mediaPlayerHolder.getSongsList().get(songIterator).getSongName())
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setWhen(0)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
            .setOngoing(true) //user can't remove notification
            .addAction(R.drawable.ic_previous, "Previous", prevPendingIntent) // #0
            .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)  // #1
            .addAction(R.drawable.ic_next, "Next", nextPendingIntent)     // #2
            .setStyle(new NotificationCompat.BigTextStyle())
            .setContentIntent(pendingIntent); //on click go to app intent
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    // notificationId is a unique int for each notification that you must define
    notificationManager.notify(1, mBuilder.build()); //show notification

2 个答案:

答案 0 :(得分:1)

您的代码没有任何问题。如果您在库存android上的模拟器中运行代码,则操作将按预期显示。

在Android上显示通知由启动器完成。像任何其他应用程序启动器可以包含错误。在这种情况下,您用于在通知中显示多个操作的启动器中存在错误。

答案 1 :(得分:-2)

我建议你试试谷歌样本环球音乐播放器这是一个GitHub link。它包括你在通知中想要的所有内容,它有最新的通知Android Oreo功能只是尝试它。