Doubt with Notification and Alarmmanager

Asked

Viewed 484 times

0

What I need is a hint, help to know how to implement an Alarmmanager that whenever a notification appears something like this

In the Home screen of the phone and is making a sound every 5s

for the notification I have the following code

protected void criarNotificacao(Context context, MensagemAlerta messagesAlerts, Class<?> activity) {

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Notification notificaction = new Notification(R.drawable.ic_launcher,
            messagesAlerts.getTitle(), System.currentTimeMillis());

    notificaction.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "6");

    notificaction.flags |= Notification.FLAG_INSISTENT;

    notificaction.flags |= Notification.FLAG_AUTO_CANCEL;

    PendingIntent p = PendingIntent.getActivity(this, 0,
            new Intent(this.getApplicationContext(), MyActivity.class), 0);

    notificaction.setLatestEventInfo(this, messagesAlerts.getSubTitle(),
            messagesAlerts.getBody(), p);

    notificaction.vibrate = new long[] { 100, 1000, 1000, 1000 };

    notificationManager.notify(R.string.app_name, notificaction);
}

The other class

public class MensagemAlerta {

private CharSequence title;

private CharSequence body;

private CharSequence subTitle;

public MensagemAlerta(CharSequence title, CharSequence body,

                      CharSequence subTitle) {

    this.title = title;

    this.body = body;

    this.subTitle = subTitle;

}

public CharSequence getTitle() {

    return title;

}

public void setTitle(CharSequence title) {

    this.title = title;

}

public CharSequence getBody() {

    return body;

}

public void setBody(CharSequence body) {

    this.body = body;

}

public CharSequence getSubTitle() {

    return subTitle;

}

public void setSubTitle(CharSequence subTitle) {

    this.subTitle = subTitle;

}}

NOTE: I call this function from within a Thread

  • What’s the matter?

  • I need to implement an Alarmmanager call with a screen like the one I reported and vibrate, playing a 5 in 5s sound

1 answer

1

If I understand correctly, what you need is the following:

First you need to create a BroadcastReceiver for his AlarmManager call for:

public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        //Suas implementações (tocar som, vibrar, etc.)
    }
}

Don’t forget to add it to your AndroidManifest:

<receiver
    android:name="seupackage.AlarmReceiver"
    android:enabled="true"
    android:exported="false" >
</receiver>

After you created your BroadcastReceiver, make your AlarmManager call you (within your notification, for example):

private void callAlarm() {
    //Definindo o intervalo de 5 segundos
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.SECOND, 5);

    //Criando sua PendingIntent com o BroadcastReceiver que você criou
    Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);

    PendingIntent pendingAlarm = PendingIntent.getBroadcast(
            getApplicationContext(),
            0,
            alarmIntent,
            PendingIntent.FLAG_CANCEL_CURRENT
    );

    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE);

    //Tempo que você quer que seu alarme se repita (em milisegundos)
    final long REPEAT_TIME = 1000 * 5;

    //Definindo o loop do Alarme
    manager.setRepeating(
            AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(),
            REPEAT_TIME,
            pendingAlarm
    );
}

Browser other questions tagged

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