Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过通知中的按钮播放上一首/下一首歌曲_Java_Android_Notifications_Android Broadcast - Fatal编程技术网

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

Java 通过通知中的按钮播放上一首/下一首歌曲,java,android,notifications,android-broadcast,Java,Android,Notifications,Android Broadcast,我有一个播放器,当用户选择歌曲时,播放器用3个按钮发出通知:上一个、播放、下一个。在后台模式下选择歌曲时需要使用此按钮。但当我点击这个按钮时,什么都没有发生。以下是通知代码: Intent intentPrev = new Intent(this, NotificationReceiver.class); intentPrev.setAction(ACTION_PREV); LocalBroadcastManager.getInstance(this).sendB

我有一个播放器,当用户选择歌曲时,播放器用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);
这是接收器的代码:

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();
        }
    }
}
我还向清单中添加了接收者:

<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>


当我在debugger中观看时,我看到没有使用Receiver类,但我不明白为什么现在,您会立即发送一个广播,其中包含以下语句:

LocalBroadcastManager.getInstance(this).sendBroadcast(intentNext);
你说你想以后开始一个
活动

PendingIntent pendingIntentNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);
(旁注:这无论如何都不起作用,因为在intentNext中,您指定了一个从
BroadcastReceiver
扩展的类,而不是从
Activity

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

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

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