• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I have a player, where when user choose a song, player makes a notification with 3 buttons: previous, play, next. This buttons are needed to choose the songs in background mode. But when I click on this buttons nothing is happened. Here's the code of notification:

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

And here's the code of 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();
        }
    }
}

Also I added receiver to 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>

When I watched in debugger I saw that Receiver class isn't used, but I don't understand why

Right now, you are sending a broadcast immediately with statements like this:

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

And you say you want to start an Activity later on:

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

(Side note: this won't work anyway because in intentNext, you specify a class extending from BroadcastReceiver not from Activity)

It should be getBroadcast() not getActivity():

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

Another thing: you are using '0' as request code (second parameter) in all your PendingIntents. You have to use different values for the request codes because two Intents sharing the same request code are considered to be the same Intent, so you'd end up with all three notification Buttons triggering the same action.

  • 1
Reply Report