Good,
I have an application that uses a layout in the barra de notificaciones
. Inside the layout, I have a botón
with an icon which, if I put it, is abre la aplicación
with the main activity. The problem arises, because what it opens is as if it were a new activity, in which everything I have done is lost. I would like that when the button is pressed, what I already had is reopened and the notification bar is retracted.
The button code is as follows:
private void openActivity(){
Intent myIntent = new Intent(parent, MainActivity.class);
myIntent.putExtra("key", "open"); //Optional parameters
parent.startActivity(myIntent);
}
But as I said before, this only opens the 0 application, however if I am outside the app and press the square button that shows the open applications and then press my app, it opens how I want.
so necesito que el botón abra la aplicación que ya está abierta en segundo plano
.
This is my class for the notification bar:
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);
}
}
Greetings, I am learning a lot with you.
I don't know how you are creating your notification, but in my case I am using a Service in android to create the notification. The intents are used to change activities and always create the activity from scratch, something you can try is to send the necessary parameters to continue where you were. For example, if I am creating a music player, from the notification button I can send the following parameters: the song that is being played, the name of the artist and the current time of the song being played.
}
In the manifest put the activity: android:launchMode="singleInstance"