1

So I am developing a simple app that takes in dates and names the user inputs and then reminds you when your friends' birthdays are. The one issue I cannot solve is how to check daily for which birthdays are today.

I have tried WorkManager and AlarmManager and NEITHER OF THEM will execute the code needed. All that needs to be done is execute a method in a java class, and the notifications will be built and sent.

Here is the code that sets the reminder. What's in the reminder is irrelevant, all that needs to be done is to run RemindWorker once a day

    public void setReminder() {
        Calendar currentDate = Calendar.getInstance();
        Calendar dueDate = Calendar.getInstance();
        dueDate.set(Calendar.HOUR_OF_DAY, 0);
        dueDate.set(Calendar.MINUTE, 0);
        dueDate.set(Calendar.SECOND, 0);
        if(currentDate.after(dueDate)) {
            dueDate.add(Calendar.Hour, 24);
        }
        long timeDiff = dueDate.getTimeInMillis() - currentDate.getTimeInMillis();
        Log.d("ReminderTests", "Time until next alarm//alarm set: " + timeDiff);
        OneTimeWorkRequest timedRemindRequest = new OneTimeWorkRequest.Builder(RemindWorker.class)
                .addTag("CALENDAR_BASED_PERIODIC")
                .setInitialDelay(timeDiff, TimeUnit.MILLISECONDS)
                .build();
        WorkManager.getInstance().enqueueUniqueWork("REMIND_WORK", ExistingWorkPolicy.REPLACE, timedRemindRequest);
    }
}

This is first called when the app is opened. at the end of the RemindWorkr class, setReminder(); is called again, starting the loop.

When the phone is on and plugged into the computer (as well as the app open), the RemindWorker will fire flawlessly and remind the user. AS SOON as the app is exited (not even cleared, just I opened another app) nothing happens at all. No reminders will fire.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • read about android services – tgkprog Jul 12 '19 at 13:29
  • I did, and services can only be run once to my knowledge. How do you loop them? – Varun Ananth Jul 12 '19 at 13:59
  • that is code inside your service. Use a jaav thread pool or timer (https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) schdule tasks in it – tgkprog Jul 12 '19 at 14:31
  • https://stackoverflow.com/questions/3819676/android-timer-within-a-service/3819721 for interacting with UI from background threads – tgkprog Jul 12 '19 at 14:33
  • @tgkprog thanks so much for your help. I was able to get the service running and looping using a repeated TimerTask that is re-started at the end of every WorkManager completion. My only issue now is that if I dismiss the app (swiping up to clear) everything stops. Yet apps like Instagram will continue to send notifications even if I was to clear them. How can i make sure that the timer doesn't stop even when the app is dismissed? – Varun Ananth Jul 13 '19 at 11:23
  • @tgkprog nevermind the whole thing isnt working again. The workanager stops calling startservice after one run – Varun Ananth Jul 13 '19 at 11:40
  • https://stackoverflow.com/a/52258125/1643558 i think all the answers are there. jsut google a bit more, and read up on them :-) – tgkprog Jul 13 '19 at 17:44
  • @tgkprog thank you so much for your help, but now there is a new hurdle. Using the info on that page, I was able to make a broadcast-handler that restarts my service when it is destroyed. the problem is, if the app is killed/in the background, I am not allowed to start a service. Is there any way around this? – Varun Ananth Jul 23 '19 at 20:31

0 Answers0