Good, first I want to make a brief summary to put in situation.
My app loads a Fragment
, I instantiate an object from a clase
that I created. This clase
contains the attributes and methods necessary to play a radio in Streaming.
In Fragment
I make calls to objeto
methods and it works correctly.
Also in Fragment
instancio a clase
called NotificationPanel
, which happened the objeto
of the radio. This clase
has everything necessary or that I think, to be able to visualize in the notification bar a layout
with the buttons of the remote control of the radio in Streaming.
The code is as follows:
public class NotificationPanel {
private Context parent;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;
private RadioOnline radio;
public NotificationPanel(Context parent, RadioOnline radio) {
// TODO Auto-generated constructor stub
this.parent = parent;
this.radio = radio;
nBuilder = new NotificationCompat.Builder(parent)
.setContentTitle("Mi 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){
//listener icono
Intent icon = new Intent(parent, NotificationReturnSlot.class);
icon.putExtra("DO", "icon");
PendingIntent btnIcon = PendingIntent.getActivity(parent, 0, icon, 0);
view.setOnClickPendingIntent(R.id.imageButtonNbarLogo, btnIcon);
//listener 1
Intent play = new Intent(parent, NotificationReturnSlot.class);
play.putExtra("DO", "play");
PendingIntent btnPlay = PendingIntent.getActivity(parent, 0, play, 0);
view.setOnClickPendingIntent(R.id.btnPlayNbar, btnPlay);
//listener 2
Intent stop = new Intent(parent, NotificationReturnSlot.class);
stop.putExtra("DO", "stop");
//stop.putExtra("DO2", radio);
PendingIntent btnStop = PendingIntent.getActivity(parent, 1, stop, 0);
view.setOnClickPendingIntent(R.id.btnStopNbar, btnStop);
}
/**
* Método que cierra el NotificationBar
*/
public void notificationCancel() {
nManager.cancel(2);
}
}
As can be seen, in the método
setListener, a Intent
is created for each button in the layout
. These Intent
are passed to the clase
NotificationResultSlot. Which analyzes that Intent has been received and executes an action.
The code is as follows:
public class NotificationReturnSlot extends Activity {
private NotificationReturnSlot ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ctx = this;
String action = (String) getIntent().getExtras().get("DO");
if (action.equals("play")) {
Log.i("NotificationReturnSlot", "play");
} else if (action.equals("stop")) {
Toast.makeText(this, "Botón stop pulsado", Toast.LENGTH_LONG).show();
Log.i("NotificationReturnSlot", "stopNotification");
}
finish();
}
}
The two problems I have are the following:
First, I do not know if this works, since I have a Toast
in the action of the Stop button, and it does not show when I click it.
The second thing is that I do not see how to pass the radio object, by the Intent, because if I pass it stop.putExtra("DO2", radio);
It tells me that I have to do Cast
a Parceable
or Seralizable
, because when I do it and I execute it with any of the two Cast
, it tells me that the object can not be converted.
What can I do to solve this? Could it be that I am posing badly how to program for android?
I hope I have been concise and have explained to me well what the problem is. Thank you very much in advance.