PendingIntent works well the first time but then its actions are repeated twice as many times

0

I am making a custom notification for a player, I use a method to create the notification works well the first time but when sending data to the notification by the MainActivity to change the name of the song the button works as if I pressed them twice but, therefore if I want to pause a song this is paused and resumed too fast.

Method

private void notification(String titulo){

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Intent previousIntent=new Intent(Constantes.ACTION.PREV_ACTION);
    ppreviusIntent=PendingIntent.getBroadcast(this,0,previousIntent,0);

    Intent playIntent = new Intent(Constantes.ACTION.PLAY_ACTION);
    pplayIntent = PendingIntent.getBroadcast(this, 0, playIntent,0);

    Intent nextIntent = new Intent(Constantes.ACTION.NEXT_ACTION);
    pnextIntent = PendingIntent.getBroadcast(this, 0, nextIntent, 0);



    remoteViews=new RemoteViews(getPackageName(),R.layout.notification);

    remoteViews.setTextViewText(R.id.Titulo,titulo);
    remoteViews.setImageViewResource(R.id.cover,R.drawable.music_player);
    remoteViews.setImageViewResource(R.id.previus_notification,R.drawable.previus_notification);
    remoteViews.setImageViewResource(R.id.play_notification,R.drawable.pause_notification);
    remoteViews.setImageViewResource(R.id.next_notification,R.drawable.next_notification);
    remoteViews.setImageViewResource(R.id.close_notification,R.drawable.close);

    remoteViews.setOnClickPendingIntent(R.id.previus_notification,ppreviusIntent);
    remoteViews.setOnClickPendingIntent(R.id.play_notification,pplayIntent);
    remoteViews.setOnClickPendingIntent(R.id.next_notification,pnextIntent);


    notification = new NotificationCompat.Builder(this)
            .setContentTitle("IMAPLAYER")
            .setTicker(titulo)
            .setSmallIcon(R.drawable.music_folder)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .setContent(remoteViews).build();


    previus=new IntentFilter();
    play=new IntentFilter();
    next=new IntentFilter();

    previus.addAction(Constantes.ACTION.PREV_ACTION);
    play.addAction(Constantes.ACTION.PLAY_ACTION);
    next.addAction(Constantes.ACTION.NEXT_ACTION);

    receiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Constantes.ACTION.PLAY_ACTION)){

                if(MyMediaSingleton.getInstance().player.isPlaying())
                {
                    MyMediaSingleton.getInstance().player.pause();
                    PAUSE++;
                }
                else
                {
                    MyMediaSingleton.getInstance().player.start();
                    PLAY++;

                }
            }
            Toast.makeText(ForegroundService.this, "PLAY:"+PLAY+" "+" PAUSE: "+PAUSE, Toast.LENGTH_SHORT).show();
        }
    };
    registerReceiver(receiver,play);

    startForeground(Constantes.NOTIFICATION_ID.FOREGROUND_SERVICE,notification);
}

Service

public class ForegroundService extends Service {


private static final String LOG_TAG = "Service";
public static boolean IS_SERVICE_RUNNING = false;


private Notification notification;
private NotificationManager notificationManager;
private RemoteViews remoteViews;

private BroadcastReceiver receiver;
private IntentFilter previus;
private IntentFilter play;
private IntentFilter next;

private PendingIntent ppreviusIntent;
private PendingIntent pnextIntent;
private PendingIntent pplayIntent;

private int PLAY=0;
private int PAUSE=0;



@Override
public  void onCreate(){
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent,int flags,int StarId){

    if (intent.getAction().equals(Constantes.ACTION.STARTFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Start Foreground Intent ");
        String titulo=intent.getExtras().getString("key");
        notification(titulo);

        Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();
    }


     return START_NOT_STICKY;
}

@Override
public void onDestroy(){
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

Data pass

public void SendToService(){
    Intent service=new Intent(MainActivity.this,ForegroundService.class);
    if(ForegroundService.IS_SERVICE_RUNNING){
        String path=recibidor_file.get(recibidor_position).getName();
        service.setAction(Constantes.ACTION.STARTFOREGROUND_ACTION);
        ForegroundService.IS_SERVICE_RUNNING=true;
        service.putExtra("key",path);
        startService(service);
    }
}

Thank you in advance for the help.

    
asked by Felipe Peña 13.11.2017 в 23:30
source

1 answer

1

I could solve it I needed the SendToService to send the data to a specific Action and therein to update it with starforeground, to avoid creating a new notification

SentToService

public void SendToService(){
    Intent service=new Intent(MainActivity.this,ForegroundService.class);
    if(ForegroundService.IS_SERVICE_RUNNING){
        String path=recibidor_file.get(recibidor_position).getName();
        service.setAction(Constantes.ACTION.DATA_ACTION);
        ForegroundService.IS_SERVICE_RUNNING=true;
        service.putExtra("key",path);
        startService(service);
    }
}

Using ACTION.DATA_ACTION would tell the server to take a specific action

Server

  if(intent.getAction().equals(Constantes.ACTION.DATA_ACTION)){
        titulo=intent.getExtras().getString("key");
        remoteViews.setTextViewText(R.id.Titulo,titulo);
        startForeground(Constantes.NOTIFICATION_ID.FOREGROUND_SERVICE,notification);
        Toast.makeText(this,"Sent data:"+titulo,Toast.LENGTH_SHORT).show();
    }

With the intent.getExtras I took the data sent, the remoteview sent them to the notification and the starforeground updated them, so I did not have the need to recreate another notification

    
answered by 14.11.2017 / 01:24
source