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.