Emil Hansen
Emil Hansen

Reputation: 71

AlarmManager gets send on initial setup

I am having trouble with my app, that on first launch i want to setup an alarmintent and I want the alarm manager to send me a notification every 24 hour at a specific time. Every thing about it works, except when i launch the app for the first time (when data is cleared from the app), then there is send a notification right away, which it should not do until the next time the clock hits 09:00.

Here is the function that setup the alarm (only called the very first time the app is running)

public void setAlarm(){
    Toast.makeText(this, "setAlarm()", Toast.LENGTH_LONG).show();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    Intent alarmIntent = new Intent(this, AlertReceiver.class);

    if(PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT) != null){
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

And here is the notification function of the broadcast receiver

public void createNotification(Context context, String msg, String msgText, String msgAlert){
    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.drawable.ic_stat_dmq_notification_icon);
    mBuilder.setTicker(msgAlert); //Ticker!
    mBuilder.setWhen(System.currentTimeMillis());
    mBuilder.setContentTitle(msg); //Title:
    mBuilder.setContentText(msgText); //Text
    mBuilder.setContentIntent(notificIntent);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(uniqueId, mBuilder.build());

I have been looking at this for days, and I cannot seem to figurer it out nor can i find a solution. If anyone can help I would be a happy man!

Upvotes: 0

Views: 66

Answers (2)

Emil Hansen
Emil Hansen

Reputation: 71

Okay i manage to figure it out after all. Only thing needed to be added was after alle the calendar.set was

if(calendar.getTimeInMillis() < System.currentTimeMillis()){
            Toast.makeText(this,"1 day have been added", Toast.LENGTH_SHORT).show();
            calendar.add(Calendar.DATE, 1);
}

It basically just check to see if the time was in the past, and if it was, then it adds another day, so the alarm will go off the next day.

Upvotes: 0

Peter Chaula
Peter Chaula

Reputation: 3731

Is it past 9am in your area. That might be the source of your error. An Alarm set to a time in the past in android fires off right away. Try:

  calendar.set(Calendar.YEAR, 2017);

Insane just for testing

Upvotes: 1

Related Questions