gpt4 book ai didi

java - 通过通知中的按钮播放上一首/下一首歌曲

转载 作者:行者123 更新时间:2023-11-30 10:06:49 25 4
gpt4 key购买 nike

我有一个播放器,当用户选择一首歌曲时,播放器会通过 3 个按钮发出通知:上一首、播放、下一首。在背景模式下选择歌曲需要此按钮。但是当我点击这个按钮时什么也没有发生。这是通知代码:

Intent intentPrev = new Intent(this, NotificationReceiver.class);
intentPrev.setAction(ACTION_PREV);
LocalBroadcastManager.getInstance(this).sendBroadcast(intentPrev);
PendingIntent pendingIntentPrev = PendingIntent.getActivity(this, 0, intentPrev, PendingIntent.FLAG_UPDATE_CURRENT);

Intent intentPlay = new Intent(this, NotificationReceiver.class);
intentPlay.setAction(ACTION_PLAY);
LocalBroadcastManager.getInstance(this).sendBroadcast(intentPlay);
PendingIntent pendingIntentPlay = PendingIntent.getActivity(this, 0, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);

Intent intentNext = new Intent(this, NotificationReceiver.class);
intentNext.setAction(ACTION_NEXT);
LocalBroadcastManager.getInstance(this).sendBroadcast(intentNext);
PendingIntent pendingIntentNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.notification).
setContentTitle(songs.get(songPos).getTitle()).setContentText(songs.get(songPos).getArtist()).
addAction(R.drawable.previous, "Previous", pendingIntentPrev).addAction(R.drawable.pause, "Pause", pendingIntentPlay).
addAction(R.drawable.next, "Next", pendingIntentNext);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);

Receiver 的代码如下:

package asus.example.com.player;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.util.Objects;

public class NotificationReceiver extends BroadcastReceiver {

private final String ACTION_PREV = "PREVIOUS";
private final String ACTION_PLAY = "PLAY";
private final String ACTION_NEXT = "NEXT";
private final MyService service = new MyService();

@Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), ACTION_PREV)){
service.playPrev();
}
else if (Objects.equals(intent.getAction(), ACTION_NEXT)){
service.playNext();
}
}
}

我还向 Manifest 添加了接收器:

<receiver
android:name=".NotificationReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="PREVIOUS"/>
<action android:name="PLAY"/>
<action android:name="NEXT"/>
</intent-filter>
</receiver>

当我在调试器中观察时,我看到没有使用 Receiver 类,但我不明白为什么

最佳答案

现在,您正在立即发送广播,其中包含如下语句:

LocalBroadcastManager.getInstance(this).sendBroadcast(intentNext);

你说你想稍后开始一个Activity:

PendingIntent pendingIntentNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);

(旁注:这无论如何都行不通,因为在 intentNext 中,您指定了一个从 BroadcastReceiver 而不是 Activity 扩展的类)

应该是 getBroadcast() 而不是 getActivity():

Intent intentNext = new Intent(this, NotificationReceiver.class);
intentNext.setAction(ACTION_NEXT);
PendingIntent pendingIntentNext = PendingIntent.getBroadcast(this, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);

另一件事:您在所有 PendingIntent 中使用“0”作为请求代码(第二个参数)。您必须对请求代码使用不同的值,因为共享相同请求代码的两个 Intent 被认为是相同的 Intent,因此您最终所有三个通知 Button 都会触发相同的操作。

关于java - 通过通知中的按钮播放上一首/下一首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54486023/

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