Stop notification (ringtone)

4

I have an Android application that wakes up with the standard notification system ringtone. The problem is that when it clicks the notification the alarm does not stop.

I use a class for notification and one with BroadcastReceiver

public class LembreteSessao extends BroadcastReceiver {
private static final String TAG = "Sessao";
public static final String ACTION = ".Alarm.Lembretes.LEMBRE_SESSAO";
private int codigo;
Ringtone toque = null;
//CLASSE CHAMADA PELO MANIFEST NO HORARIO DO ALARME
@Override
public void onReceive(Context context, Intent intent) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    int Hora = calendar.get(Calendar.HOUR_OF_DAY);
    int Minu = calendar.get(Calendar.MINUTE);

    Date time = new Date(00, 00, 00, Hora, Minu);
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    String currentTime = timeFormat.format(time);

    String config = this.verificarAlerta(context);
    if (config.equals("A")){
        //toque do sistema
        Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        toque = RingtoneManager.getRingtone(context, som);
        toque.play();
    }
    if (config.equals("B")){
        //vibrar suave
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(1000);
        Log.d(TAG, "Sessao: " + new Date());
    }
    if (config.equals("C")){
        //vibrar intenso
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(3000);
        Log.d(TAG, "Sessao: " + new Date());
    }
    if (config.equals("D")){
        //tocar e vibrar
        Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        toque = RingtoneManager.getRingtone(context, som);
        toque.play();

        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);
        Log.d(TAG, "Sessao: " + new Date());
    }

    codigo = intent.getIntExtra("ID_SESSAO", 0);
    Intent notifIntent = new Intent(context, lembrete_sessao.class);

    notifIntent.putExtra("ID_SESSAO",codigo);
    notifIntent.putExtra("TIME", currentTime);

    NotificationUtil.create(context, 3, notifIntent, R.mipmap.ic_launcher, "Sessao", "Verifique a Sessao");

}

I also use notification:

public static void create(Context context, int id, Intent intent,int smallIcon, String contentTitle, String contentText) {

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Intent para disparar o broadcast

    PendingIntent p = PendingIntent.getActivity(context, id, intent, 0);

    // Cria a notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setTicker("");
    builder.setContentTitle(contentTitle);
    builder.setSmallIcon(smallIcon);
    builder.setContentIntent(p);
    builder.setContentText(contentText);

    // Dispara a notification
    Notification n = builder.build();
    n.flags = Notification.FLAG_AUTO_CANCEL;

    manager.notify(R.drawable.ic_launcher, n);     

    Log.d(TAG, "Notification criada com sucesso");
}

How do I, when the user clicks on the notification, the Ringtone ends? I searched for something like onDestroy already on the Receiver, but I did not find it and I do not know if it works like this.

    
asked by anonymous 31.12.2015 / 11:00

1 answer

3

Set the notification so that it is performing the ringing and vibration.

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
....
....
//Definir o toque
Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(som);

//Definir vibração
long[] pattern = {0,1000};//Vibra durante 1000 milisegundos
builder.setVibrate(pattern);
    
31.12.2015 / 12:12