How do I make a daily action(Android Studio) - android

I recently started learning about app programing, and I am having a problem.
I want to know how can i make my app do a certain action every time a day is passing.
For example, If i have an int parameter that Equals 10 and today is Sunday.
I need that on Monday the Parameter will be 9,
and on the next day 8, and so on....
Can anyone help me with this?

If you want your app to start every x time, you should use AlarmManager for that and also consider registering for boot_complete broadcast.
If you simply need this for calculation, you should use System.getCurrentTimeMillis to get the current time and make your decisions.

Related

Android : Decrement a variable daily

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.

ACTION_TIME_CHANGED Intent, can we set this to trigger based on a "set time"?

Just a quick question about this, for the android platform, I was directed to this link from another question..
Is there a way to detect when the user has changed the clock time on their device?
If the player changes their system time, this event would trigger. I would only like this event to trigger if the time changed is greater than 40 seconds, is this possible?
Is there a way to make this trigger for certain times only? for example, 30-40 seconds is no big deal for me, however anything over that, I would like this to fire, and then check in with a server to see if the user has tampered with their device time?
I realize there will be other things to worry about such as timezones, I think I can work that out if it is possible for this to only fire if the time changed is greater than 40 seconds.
This is from the documentation.. suggesting that this is not possible. Can anybody clarify?
public static final String ACTION_TIME_CHANGED
Added in API level 1
Broadcast Action: The time was set.
Constant Value: "android.intent.action.TIME_SET"
No, that isn't possible. This broadcast is sent when the time is set (changed). If you are listening for this broadcast Intent you will get it every time. There isn't a way to tell Android that you only want to get triggered under certain circumstances.

Run a function on the first day of every month

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.

Using AlarmManager to print a message once a day

I'm new to android development and I would like to know how to get the app to do a certain action at a certain time of each day. For example incrementing a counter every time it hits midnight. I spent quite some time yesterday looking into this but couldn't find anything straight forward.
Thanks
For the use of an AlarmManager the following Tutorial is very helpful
http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/

Need to know when it's a new day, i.e., when the time is 00:00:00

I think I read somewhere that there's a system event for when the time changes (each second?) but are there other levels of granularity such as when the hour changes?
I have a TabActivity for a 7 day TV Guide - the indicator for the first (leftmost) always shows 'Today' with the others showing a three-letter day (Mon, Tue, Wed etc) obviously depending on what the day is today.
What I'd like to do is have the activity 'know' when midnight comes so it pops up a 'Re-loading please wait...' ProgressDialog and adjusts the indicator on each tab and the associated tab contents.
Is there a built-in system event that can help or is AlarmManager my best approach?
Your best bet is probably AlarmManager with a PendingIntent that sends a broadcast. Then just implement a BroadcastReceiver to catch the broadcast and update your app. Personally, I've never heard of a built-in broadcast for time changes, but if there is one, you could always just use an if statement to find out if it's the next day.

Categories

Resources