Stop touch(sound) of notification

Asked

Viewed 292 times

4

I have an Android app that wakes up with the system notification standard ringtone. The problem is that when it clicks on 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");

}

And 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 make sure that when the user clicks on the notification, Ringtone finishes? I already looked for something like onDestroy in Receiver, but I didn’t find and I don’t know if fuciona.

1 answer

3


Configure the notification so that it is it that performs the touch 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.