Heyyy, I know how to save variables and other data in SharedPreferences, but I like to know how it would be possible to decrement a variable daily.
Already this var (int) will be in the Shared Preferences, and every day we decrements of -1.
How could I decrement, knowing that the user does not necessarily open the app every day for example?
Have a good day :)
Save the original date, when the application is opened check for current date and you know what your variable should be :)
If you want to decrement the variable on Daily Basis then
Implement a BroadcastReceiver and declare it in AndroidManifest.xml with action android.intent.action.DATE_CHANGED. This would run the BroadcastReceiver once in a day when the date is changed.
In onReceive() method you can place code (validations) and persist results into SharedPrefs.
Note:
1. Service would incur huge running cost.
2. Activity would run the same code everytime it opens but BroadCast would run it once a day in a clean and independent fashion.
You might need a Broadcast action BOOT_COMPLETED as if the device is turned on (reboot).
You have several choices:
Every time your app starts, load the value from Shared Preferences and decrement it by the number of days which have passed.
Write a service which decrements the values every day.
Write a BroadcastReceiver which starts when the date changes.
I think using JobScheduler would be a nice way to implement this. It's available from API 21 and allows the app to execute actions under certain circumstances, even if it's not open. Here's the official doc, and a nice tutorial here.
Related
I am trying to introduce auto-save functionality on one of my Android applications. The idea is that as the user inputs first name, last name and after a fixed interval I would like to save that information to the server before the user hits Next button. The final goal is to have something similar to the draft option in the Gmail app where your email information is automatically saved. So, if there is a timer that runs every 10 seconds, I will pass the information on the screen to the ViewModel and let it deal with the logic of saving the data to the server.
A couple of options I have explored are.
Execute recurring code with a specified interval using Handler.
PeriodicWorkRequest -- however this option has a minimum interval of 15 minutes which is a little too much for my use case.
AlarmManager -- This option runs even if your application is not currently running, In my opinion, this option can be an overkill.
I wanted to know if there are best practices/blogs around this and if anyone I on the wrong path or potential red flags with this approach.
you can make countdown for 10 second, when countdown is down save the data and call the countdown again.
when your activity is destroyed, so stop the countdown
This question is vague but I am not sure what to Google for exactly.
But in my app I have a section where you create a list of tasks with various attributes, and a lot of these numbers are summed up and displayed in daily totals visually. But these daily totals should reset every 24 hours (based on the hour of the day the user chooses for the cutoff, e.g. 3 am if someone works late).
Right now: my database can hold all the data by day. Then my daily counters will visually display the numbers by pulling the corresponding data from the database looking for the current day. That's the easy part.
The hard part: I can refresh the counter after the time cutoff if the user rotates the screen or restarts the app because then it'll be looking for items in the database with a new day that won't be found, so everything will be 0 as intended. But what if the user is just staring at the screen as the time cutoff rolls by? How do I make the app refresh the counters once the time hits? What if they're not even using the app at all (either it's minimized in the background or not even active).
Do I need to have some kind of always-running loop in the background that checks the current time against the values in the database? If so, isn't this inefficient if it's always needing to pull values from a database based on time? What's the correct practice for something like this?
You can setup a service and schedule that service to run periodically so that it does whatever job you want it to do
maybe this article can help you.
Alarm manager and services will be ideal for you to implement to do something for your requirement.
Services : It will be running all the time irrespective of your life-cycle of activity.
Alarm manager: Inside services use alarm manager to trigger event to update UI at regular interval.
Finally you can use Local braodcast reciever to update your Activity from services.
You can check implemetation in details below :
Android update activity UI from service
my problem is that I want to run a service that compares the current time of the device. And a "x" hour make some things.
But I don't have idea how can I make that.
Thank you a lot
Schedule a repeating alarm using AlarmManager (see this training lesson).
Then in the BroadcastReceiver, compare the current time with the list of times when you want to do some things if matches do it.
You can start a thread in your service which checks the time at particular intevrvals. These intervals can be defined by you by putting this statement:
Thread.sleep(time_in_milliseconds);
in your Thread's "run" method.
You can then use the SimpleDateFormat class to get the current time and compare hours with desired value. Once it is equal to the desired value, you can perform whatever task you wanted to do. Consider going through the SimpleDateFormat oracle docs for more detail and learning about Threads in case you don't know anything about them.
I want to run a function from my Android application at a fixed time (let's say 8am) on the first day of every month. The function, depending on user's preferences, will either serve a notification or start downloading a file over the Internet (both of which are already taken care of) or do nothing.
I tried using BroadcastReceiver with action android.intent.action.DATE_CHANGED but I read that it is fired only when user changes the date manually (although even then it worked the first 2-3 times I tried and then stopped working). I think an AlarmManager will be able to do what I need done but not really able to figure out how to implement it.
This should run whether or not my application is active/running. What is the optimum way to do this? Any supporting code will be appreciated.
Just start a service when start up. In the service,you could check the date in another thread.
Use an alarm
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()/*now*/, TIME_REPEATING, pendingIntent);
The TIME_REPEATING will be your calculate of the first day on the month, and the pendingIntent your service to run the task.
The unique hard work will be know how many time need to pass to the first day of the next month. But it's possible and very pausable to calculate.
I wanted add application signature for some application, which will be valid only for 1 or 2 days. I did enough googling but did not find enough info. So Please let me know how can i make a application get expired in 2 days..
simply add an alarm of calculating time equals to 2 days at the very first start of the app.When the alarm gets expired you will get the callback and set a global flag as false.Code in your app that if that flag is false, display a lock screen
You could save the first start time in SharedPreferences and then on each start compare the current time with the saved one. If the 2 days have passed, you can do what ever action you want.
It will probably not be possible to automatically remove the app from the device though.
If your app sends data to a server, you should store the "first run time" there. Storing it locally on the phone (for example SharedPreferences) is not very safe as it's easily overcome by changing the phone's time settings.