Doubt with the BroadCastreceiver, do not record the actions

0

I have created a clase , which extends from BroadcastReceiver . I use this class to display a notification bar with two buttons to register each click.

The notification bar is perfectly displayed, but when I click on the buttons, it does absolutely nothing.

I would like to know how I can register the click of each button in the same class.

The code for my class is as follows:

public class NotificationPanel extends BroadcastReceiver {

private Context parent;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;
private RadioOnline radio;
private static final String MyOnClick1 = "myOnClickTagIcon";


public NotificationPanel(Context parent, RadioOnline radio) {
    // TODO Auto-generated constructor stub
    this.parent = parent;
    this.radio = radio;
    nBuilder = new NotificationCompat.Builder(parent)
            .setContentTitle("Radio")
            .setContentText("Radio Online activada")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setOngoing(true);

    remoteView = new RemoteViews(parent.getPackageName(), R.layout.notification_layout);

    if (radio.getPlayer().isPlaying()){
        Toast.makeText(parent, "Se sigue escuchando", Toast.LENGTH_LONG).show();
    }else {
        Toast.makeText(parent, "No se escucha", Toast.LENGTH_LONG).show();
    }

    //set the button listeners
    setListeners(remoteView);
    nBuilder.setContent(remoteView);

    nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(2, nBuilder.build());
}

@Override
public void onReceive(Context context, Intent intent) {
    if (MyOnClick1.equals(intent.getAction())) {
        // your onClick action is here
        Toast.makeText(context, "Button1", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button1");
    }
}

public void setListeners(RemoteViews view){
    view.setOnClickPendingIntent(R.id.imageButtonNbarLogo, getPendingSelfIntent(parent, MyOnClick1));
}

protected PendingIntent getPendingSelfIntent(Context context, String action){
    Intent icon = new Intent(context, getClass());
    icon.setAction(action);
    return PendingIntent.getBroadcast(context, 0, icon, 0);
}

/**
 * Método que cierra el NotificationBar
 */
public void notificationCancel() {
    nManager.cancel(2);
}

}

I've checked that it goes into all the methods except the onReceive. How can I make the onReceive listen and pick up the button that has been pressed?

I just need to pick up that each layout button has been pressed.

Greetings and thank you very much, I am learning a lot thanks to you.

    
asked by Natlum 15.11.2016 в 15:30
source

1 answer

0

I have already found out the problem. The solution is the following:

In the class the code would look like this:

public class NotificationPanel extends BroadcastReceiver {

private Context parent;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;
private RadioOnline radio;
private static final String MyOnClick1 = "myOnClickTagIcon";

public NotificationPanel() {
}

public NotificationPanel(Context parent, RadioOnline radio) {
    // TODO Auto-generated constructor stub
    this.parent = parent;
    this.radio = radio;
    nBuilder = new NotificationCompat.Builder(parent)
            .setContentTitle("Radio")
            .setContentText("Radio Online activada")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setOngoing(true);

    remoteView = new RemoteViews(parent.getPackageName(), R.layout.notification_layout);


    //set the button listeners
    setListeners(remoteView);
    nBuilder.setContent(remoteView);

    nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(2, nBuilder.build());
}

public void setListeners(RemoteViews view){
    view.setOnClickPendingIntent(R.id.imageButtonNbarLogo, getPendingSelfIntent(parent, MyOnClick1));
}

protected PendingIntent getPendingSelfIntent(Context context, String action){
    Intent icon = new Intent(context, getClass());
    icon.setAction(action);
    return PendingIntent.getBroadcast(context, 0, icon, 0);
}

@Override
public void onReceive(Context context, Intent intent) {
    //super.onReceive(context, intent);
    Toast.makeText(context, "onReceive in", Toast.LENGTH_LONG).show();
    if (MyOnClick1.equals(intent.getAction())) {
        // your onClick action is here
        Toast.makeText(context, "Button1", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button1");
    }

}

/**
 * Método que cierra el NotificationBar
 */
public void notificationCancel() {
    nManager.cancel(2);
}

}

Then I would open to add the following in the AndroidManifest:

<receiver android:name="ecca.radio.clasesRecursos.NotificationPanel" android:enabled="true">
        </receiver>

The main problem was that I had not declared the receiver in the AndroidManifest.

Greetings and thank you very much, I am learning a lot the beautiful art of programming thanks to you.

    
answered by 16.11.2016 / 13:53
source