如何在Lollipop中向通知添加暂停/播放按钮?

3

我有一个播放广播并创建通知的服务。通知必须包括标题、文本、时间和一个按钮(暂停/播放)。我已经添加了其余的内容,通知可以显示出来。但是我不确定如何将按钮添加到通知中。在搜索时,我发现可以使用RemoteView、RemoteControlClient和MediaStyles。但是当所有按钮(上一个、暂停、播放、后退)都在播放媒体文件时,才会使用RemoteControlClient和MediaStyles。所以我真的很困惑。有人能建议我,在Lollypop中使用哪个来在通知中添加按钮吗?


1
请使用远程视图并按照以下链接 http://www.androidbegin.com/tutorial/android-custom-notification-tutorial/ 进行操作。 - Ramesh
这对Lollypop有效吗? - Sangeetha Pinto
最近我们在一个应用程序中使用远程视图实现了通知功能。它在Lollipop版本上也能正常工作。 - Ramesh
1
使用远程视图自定义通知 - http://www.skholingua.com/android-basic/advance-topics/notifications - chiru
好的,我会使用它,并告诉您它是否有效。 - Sangeetha Pinto
@Ramesh,谢谢你。你的解决方案对我很有用。 - Sangeetha Pinto
1个回答

1

首先,你需要知道在23 API中addAction已经被弃用,请阅读this进行相关了解。

调用showNotification()方法来显示通知。

 private void showActionButtonsNotification() {


        Notification.Builder notif;
        NotificationManager nm;
        notif = new Notification.Builder(getApplicationContext());
        notif.setSmallIcon(R.mipmap.ic_launcher);
        notif.setContentTitle("Hi there!");
        notif.setContentText("This is even more text.");
        Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notif.setSound(path);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Intent yesReceive = new Intent();
        yesReceive.setAction(MyNotificationReceiver.RESUME_ACTION);
        PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
        notif.addAction(R.drawable.resume, "resume", pendingIntentYes);


        Intent yesReceive2 = new Intent();
        yesReceive2.setAction(MyNotificationReceiver.STOP_ACTION);
        PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT);
        notif.addAction(R.drawable.stop, "stop", pendingIntentYes2);




        Intent maybeReceive2 = new Intent();
        maybeReceive2.setAction(MyNotificationReceiver.CANCEL_ACTION);
        PendingIntent pendingIntentMaybe2 = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, maybeReceive2, PendingIntent.FLAG_UPDATE_CURRENT);
        notif.addAction(R.drawable.cancel, "cancel", pendingIntentMaybe2);


        assert nm != null;
        nm.notify(MyNotificationReceiver.REQUEST_CODE, notif.getNotification());


    }

创建 MyNotificationReceiver 类来接收点击。
    public class MyNotificationReceiver extends BroadcastReceiver {
        public static int REQUEST_CODE_NOTIFICATION = 1212;
        public static int REQUEST_CODE = 10;
        public static final String RESUME_ACTION = "RESUME_ACTION";
        public static final String STOP_ACTION = "STOP_ACTION";
        public static final String CANCEL_ACTION = "CANCEL_ACTION";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            Log.e("action", action);

            if (intent.getAction() != null) {
                switch (intent.getAction()) {
                    case RESUME_ACTION :
                        Toast.makeText(context, "resume", Toast.LENGTH_SHORT).show();
// you resume action
                        break;
                    case STOP_ACTION :
                        Toast.makeText(context, "stop", Toast.LENGTH_SHORT).show();
// you stop action
                        break;
                    case CANCEL_ACTION:
                        Toast.makeText(context, "cancel", Toast.LENGTH_SHORT).show();
// you cancel action
                        break;
                }
            }
        }



    }

最后,将receiver添加到您的manifests中。
<receiver android:name=".MyNotificationReceiver">
            <intent-filter>
                <action android:name="RESUME_ACTION"/>
                <action android:name="STOP_ACTION"/>
                <action android:name="CANCEL_ACTION"/>

            </intent-filter>
        </receiver>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接