how to make the code that runs at 21:00 even though no user opens the application

Viewed 78

Guys can I make a code run at 9:00 p.m. even though no user is opening my application?

I want to give a gift to each user at 21.00, can I put it in my project or I have to create a user like "Admin" who is online to keep my code running at 21.00.

I'm afraid when I enter the code into the project, and when no user opens my application, the prize can't be shared

can anyone give me an idea of ​​its making?

The Clearly question is, i want to push data to database at 21.00 automatic, even though no one has opened my application

1 Answers

To run your code exactly at 21:00 not even a min. late, use AlarmManager class like this:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // use setAlarmClock function of AlarmManager
            // but this function will show an alarm icon on statusbar
            // if you dont want to show that icon you can use
            // setExactAndAllowWhileIdle function but that will not be 
            // perfectly exact
        }
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // use setExact function of AlarmManager
        }
        else {
            // use set function of AlarmManager
        }
Related