Doubts with Notification and AlarmManager

0

What I need is a hint, it helps to know how to implement an AlarmManager that whenever something comes up it will appear something like this

OntheHomescreenofthephoneandmakeasoundevery5seconds

ForthenotificationIhavethefollowingcode

protectedvoidcriarNotificacao(Contextcontext,MensagemAlertamessagesAlerts,Class<?>activity){NotificationManagernotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);Notificationnotificaction=newNotification(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

    
asked by anonymous 18.12.2014 / 12:28

1 answer

1

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

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

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

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

Do not forget to add it to your AndroidManifest :

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

Once you have created your BroadcastReceiver , make your AlarmManager call it (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
    );
}
    
18.12.2014 / 14:53