gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:59:24 25 4
gpt4 key购买 nike

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

最佳答案

首先你需要知道 addAction 在 23 API 中被弃用,阅读 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>

关于android - 如何在 Lollipop 通知中添加暂停/播放按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28358827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com