Así que necesito que el botón abra la aplicación ya abiertanecesito que el botón abra la aplicación que ya está abierta en segundo plano
.
Esta es mi clase para la barra de notificaciones:
public class NotificationPanel extends BroadcastReceiver {
private static Context parent;
private static NotificationManager nManager;
private static NotificationCompat.Builder nBuilder;
private static RemoteViews remoteView;
private static RadioOnline radio;
private static final String MyOnClick1 = "IconButton";
private static final String MyOnClick2 = "ButtonStop";
private static final String MyOnClick3 = "ButtonPlay";
public NotificationPanel() {
}
public NotificationPanel(Context parent, RadioOnline radio) {
this.parent = parent;
this.radio = radio;
nBuilder = new NotificationCompat.Builder(parent)
.setContentTitle("Radio ECCA")
.setContentText("Radio Online activada")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_HIGH)
.setOngoing(true);
remoteView = new RemoteViews(parent.getPackageName(), R.layout.notification_layout);
setListeners(remoteView);//Llama al método que genera los botones a la escucha.
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));
view.setOnClickPendingIntent(R.id.btnStopNbar, getPendingSelfIntent(parent, MyOnClick2));
view.setOnClickPendingIntent(R.id.btnPlayNbar, getPendingSelfIntent(parent, MyOnClick3));
}
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) {
if (MyOnClick1.equals(intent.getAction())) {
Toast.makeText(context, "Icono tocado", Toast.LENGTH_SHORT).show();
Log.w("Widget", "Clicked Icon");
openActivity();
}else if (MyOnClick2.equals(intent.getAction())){
//Toast.makeText(context, "Botón Stop tocado", Toast.LENGTH_SHORT).show();
Log.w("Widget", "Clicked button stop");
try {
pararRadio();
activarPlay();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}else if (MyOnClick3.equals(intent.getAction())){
//Toast.makeText(context, "Botón Play tocado", Toast.LENGTH_SHORT).show();
Log.w("Widget", "Clicked button play");
try {
if (radio.isNetworkConnectedRemote(parent)){
Toast.makeText(context, "Hay internet", Toast.LENGTH_LONG).show();
activarStop();
}
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
public void notificationCancel() {
nManager.cancel(2);
}
private void pararRadio() throws Exception {
Log.w("Widget", "Dentro del método pararRadio");
if (this.radio.getPlayer().isPlaying())
{
Log.w("Widget", "Se está reproduciendo");
try
{
this.radio.stopPlaying();
}
catch (Exception e) {
e.printStackTrace();
throw new Exception("Se ha producido un error al parar el reproductor\n"+e);
}
}
}
private void iniciarRadio() throws Exception {
Log.w("Widget", "Dentro del método pararRadio");
if (this.radio.getPlayer().isPlaying())
{
Log.w("Widget", "Se está reproduciendo");
}else{
try
{
this.radio.startPlayingRemote();
}
catch (Exception e) {
e.printStackTrace();
throw new Exception("Se ha producido un error al inicializar el reproductor\n"+e);
}
}
}
private void activarPlay(){
Resources res = parent.getResources();
String mystring = res.getString(R.string.infoNbarStoped);
this.remoteView.setViewVisibility(R.id.btnStopNbar, View.INVISIBLE);
this.remoteView.setViewVisibility(R.id.btnPlayNbar, View.VISIBLE);
//this.remoteView.setBoolean(R.id.btnPlayNbar, "setEnabled", true);
this.remoteView.setTextViewText(R.id.messageNbar, mystring);
nManager.notify(2, nBuilder.build());
}
private void activarStop(){
Resources res = parent.getResources();
String mystring = res.getString(R.string.infoNbar);
this.remoteView.setViewVisibility(R.id.btnStopNbar, View.VISIBLE);
this.remoteView.setViewVisibility(R.id.btnPlayNbar, View.INVISIBLE);
//this.remoteView.setBoolean(R.id.btnPlayNbar, "setEnabled", true);
this.remoteView.setTextViewText(R.id.messageNbar, mystring);
nManager.notify(2, nBuilder.build());
}
private void openActivity(){
Intent myIntent = new Intent(parent, MainActivity.class);
myIntent.putExtra("key", "open"); //Optional parameters
parent.startActivity(myIntent);
}
}