Best class to schedule a task - java

I'm planning to make an app that enables the Mobile Data of the android every X hours for Y minutes. I was wondering which class should I use to schedule this task. Thanks

For tasks for which the user can give you a specific time and date, you can use AlarmManager to have your task executed at that time.
This way, your app does not need to be running the whole time, and will be launched by Android when the time comes.
You can even use the setRepeating() method to set it only once and have it run everytime.

Related

Ways to work around the Android workManager 15 minute minimum interval?

The game I am working on is a missile-oriented GPS-based combat game on Android. The app checks every 15 minutes to see if the user is under attack by any other players, and if so, sends them a notification that they are under attack. Currently, because of the minimum 15 minute interval, the app sends these notifications either too late or not at all. What I need to do is alter this so that somehow, some way, the app checks the "under attack" status of the user more often than 15 minutes. Every minute or every 30 seconds would be ideal.
here is the doWork() method which starts the notification check:
{
if(!MainActivity.GetRunning())
{
Utilities.DebugLog(context, "AlertService", "Main activity not running. Firing notification service handler.");
NotificationServiceHandler handler = new NotificationServiceHandler(context);
handler.Start();
}
return Result.SUCCESS;
}
WorkManager is not a suitable tool for what you wish to do. You will need to use a foreground service and your own in-process timing engine (e.g., ScheduledExecutorService). That will not work for very long before Doze mode and other power-saving measures take effect, but hopefully your games are only an hour or so long.
Hej theBiscuit,
instead of using WorkManager, you could set up an AlarmManager to wake up the app and check for attacks.
If you want to do it while the app is running, a CountDownTimer could help for short periods of time.

Detect user change clock time after Force Stop app

I can detect change clock time when app in foreground, background, or kill from Recent App by using android.intent.action.TIME_SET follow here.
However, if I Force Stop app in Setting->Apps I can not receive this broadcast anymore.
Currently, I want to detect user change clock time come back to my app after ForceStop so I do
long deltaTimeBeetweenCurrentTimeAndTimeSinceReboot = System.currentTimeMillis() - SystemClock.elapsedRealtime();
long oldDelta = mSharedPreference.getDeltaTimeBeetweenCurrentTimeAndTimeSinceReboot();
if(deltaTimeBeetweenCurrentTimeAndRebootTime - oldDelta > 5000){
// clock time change
}
Idea is I saved a delta between currently time (System.currentTimeMillis()) and time since reboot (SystemClock.elapsedRealtime()). Every time I open app, I will compare oldDelta and newDelta (except the first time install). It work well in case: User Fore Stop app->Change time->come back to app.
However, there is still have 1 case that is: User Fore Stop app -> Change the clock time -> Reboot device -> Open my app. At this time I can not use the above method to check the clock time have changed because after reboot the SystemClock.elapsedRealtime() will reset. How can I detect clock time have changed in that case?
Any help or suggestion would be great appreciate.
Few months ago I was in pretty the same situation. I didn't find any answer to directly solve the case, so I won't help you with it. But I can give you kind of advice:
Look at it with a different point of view.
What I did in my case, I answered myself to questions:
"Do I really want to know that user has changed the time - because I implicitly inform/present the fact to user?"
OR
"Do I want to know that user has changed the time - because I need it to invoke some actions or calculations in the application background?"
In my case I had NO-YES answers. So, for problematic case of Force-Stop + reboot I assumed that the time could had been changed and I reset my application's time configuration likewise the first app launch.
Let me know if it helps you anyway.
It would be hard to implement without external etalon (backend is the best option). You may save time a user force to stop the app and when the app alive again compare with some predefined delta (time window). If you get a "big" difference consider the user is cheating.
You may also play with timestamps of the filesystem to define some inconsistency.

Does android support real time events? If so how could they be implemented?

I'm using Android Studio to develop and application and I'd like that when a certain moment of time is met the application performs some defined action.
I know I could create a service that continously checks for actual time, every little seconds and if it happens to be equal or bigger than the needed time it performs the action, but doing it this way looks too resource consuming.
Is there some way in Android to do something like when a defined time comes, an interruption is sent and the defined action is fired?
It would be enough with supporting a soft time event, it doesn't mind for my application for the event to be fired some seconds later.
Thanks for helping.
Yes take a look at AlarmManager it will let you do an action in a specific time
If you need to check some external databases once every X seconds/minutes, then you may consider using a Handler & Runnable with postDelayed() method to achieve what you need.

Performing a task everyday using TimerTask doesn't work for the next day.(with other problems)

I have a TimerTask that updates a few tables everyday at specific times(not one task per day, different tasks at different times in a day). I get a few time periods from a database and check if the current time is equal to one of the times in the database when the run() of TimerTask is called and perform the respective task for respective time(time from database which is equal to current time)
I think it's not working for the next day because I set the time(which I obtain from database) for the timer when the service starts. The next day when the timer is supposed to do the task and checks with the current time it checks with the previous day's time(the day service is started and time is set for TimerTask) and does not do the task. I should start the Service everyday to set the time(which is obtained from database to check with current time).
How do I start the service everyday at specific time so that the time to be checked with current time is set everyday.
I can try to schedule to do the tasks everyday 24 hours from the day service is started, but sometimes I have to change the start date and for that the service should be started again and I dont know if a service will run for 24 hours.
You should use Alarmanager for this. The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

storing the count down timer value in android

As the Count Down timer pauses, how would you store the value?
which then when you want to resume the timer eventually will restart a new timer
with this stored value. I am so confused how to hold the value.
I am a beginner in java and android programming.
could any one guide me.
Android has inbuilt class for count down i.e. CountDownTimer class . You can use this.
http://developer.android.com/reference/android/os/CountDownTimer.html
Countdown timer doesn't have a pause() method. Workaround that i can think off is try getting the time at onTick().
So you have an application displaying count down timr, and like that it continues countdown from very same moment after being paused? In this case you have to save your
remaining time in some location in onPause() method ( best location would be shared preference )
Or do you like to have real countdown time and fire some event after it expires, even if your application is inactive? Then schedule an alarm at desired time, and store scheduled time somewhere (shared preferences would be ideal). In this case you can forget keeping time yourself, just update UI periodically using real time and stored time difference

Categories

Resources