1

按照指南和其他在线讨论,我制作了这段代码,应该使用 .addAction 方法显示通知中的按钮:

@SuppressLint("NewApi")
    public void generateNotificationSong()
    {
        if(android.os.Build.VERSION.SDK_INT >= 11){
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setContentTitle(songsList.get(currentSongIndex).getTitle());
            builder.setContentText("Next: " + songsList.get(getNextSong()).getTitle());
            builder.setOngoing(true); //E' in corso
            builder.setAutoCancel(true);


            //intent con activity che devo aprire (la corrente)
            Intent thisintent = getIntent();
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, thisintent, PendingIntent.FLAG_UPDATE_CURRENT);


            //Aggiungo i bottoni alla notifica se android è > 4.1
            if(android.os.Build.VERSION.SDK_INT >= 16)
            {
                Log.v("A", "CIAO");
                Intent prevIntent = new Intent();
                prevIntent.setAction("prec");
                PendingIntent pendingPrev = PendingIntent.getBroadcast(this, 123, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                Intent nextIntent = new Intent();
                nextIntent.setAction("next");
                PendingIntent pendingNext = PendingIntent.getBroadcast(this, 123, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                Intent pauseIntent = new Intent();
                pauseIntent.setAction("pause");
                PendingIntent pendingPause = PendingIntent.getBroadcast(this, 123, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                builder.addAction(R.drawable.btn_previous, "Previous", pendingPrev);
                builder.addAction(R.drawable.btn_pause, "Pause", pendingNext);
                builder.addAction(R.drawable.btn_next, "Next", pendingPause);
            }

            //la setto alla notifica
            builder.setContentIntent(pendingIntent);

            //infine la chiamo
            mNotify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            if(android.os.Build.VERSION.SDK_INT >= 16)
                mNotify.notify(001, builder.build()); //funziona meglio
            else
                mNotify.notify(001, builder.getNotification()); //deprecated
        }
    }

我得到的是没有添加按钮的通知,我想我遗漏了一些东西,但我还没有找到解决方案

4

1 回答 1

8

这是一个可能的原因。当存在其他通知时,Android 可能会隐藏按钮,尤其是在您的手机正在充电时。尝试用两根手指点击通知并向下滑动。您应该能够看到您的按钮。

于 2014-05-05T10:26:07.467 回答