I'm trying to make an app that gets notifications at user-determined times every day, but I was only able to get them to arrive once.
This is the code I have so far:
private void salvar() {
RespostasAguaCasa p = new RespostasAguaCasa();
p.setId(1);
p.setValoragua(QuintaperguntaAguaCasa.valoragua);
p.setAcordarhora(EscolhaAguaCasa.hourx);
p.setAcordarminu(EscolhaAguaCasa.minutex);
p.setDormirhora(SegundaperguntaAguaCasa.hourxx);
p.setDormirminu(SegundaperguntaAguaCasa.minutexx);
p.setTerceirahora(TerceiraperguntaAguaCasa.hourxxx);
p.setTerceiraminu(TerceiraperguntaAguaCasa.minutexxx);
p.setQuartahora(QuartaperguntaAguaCasa.hourxxxx);
p.setQuartaminu(QuartaperguntaAguaCasa.minutexxxx);
p.setQuintahora(QuintaperguntaAguaCasa.hourxxxxx);
p.setQuintaminu(QuintaperguntaAguaCasa.minutexxxxx);
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.setTimeInMillis(System.currentTimeMillis());
calSet.set(Calendar.HOUR_OF_DAY , EscolhaAguaCasa.hourx);
calSet.set(Calendar.MINUTE, EscolhaAguaCasa.minutex);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
setAlarm(calSet);
}
Alarm:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(arg0)
.setSmallIcon(R.drawable.casa)
.setContentTitle("ads")
.setContentText("sad")
.setAutoCancel(true)
;
NotificationManager notificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}
setAlarm
is in the same class as salvar
:
public void setAlarm(Calendar targetCall)
{
Toast.makeText(this, "Alarm is set at" + targetCall.getTime(),
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingintent = PendingIntent.getBroadcast(getBaseContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCall.getTimeInMillis(), pendingintent);
}