I'm looking for some ways to make an app that every body can use all the futures for just 3 days, after the limited time ends some of futures disables but again via In-App Purchase they will ready to use.
The question is, How can I set a timer to count down for 3 days from the time that application for first time opened? I should do it in some way that when the phone power offs there mustn't be problem.again after powering on and without opening the app it should count down to 00:00:00.
Is there any samples for this? Or how can I do it?
As #4castle said, set a time stamp when they first open the app. Then, if you want to display a countdown with an exact time until expiration, have a text box somewhere that displays time remaining. Calendar
import java.util.Calendar
if( this.firstTimeRunning() ){
Calendar start = Calendar.getInstance();
//save start.getTimeInMillis()
}
Calendar end = Calendar.getInstance();
end.setTimeInMillis( /* saved millisec value */);
end.set(Calendar.DAY, end.get(Calendar.DAY) + 3);
long difference = end.getTimeInMillis() - start.getTimeInMillis();
text_box.setText(/* format difference*/); //update every second
Something like that.
Related
I am trying to make a Timer that will start counting from Date,
so every time i launch the app, the Timer will always be updated
for example if i start the timer at 20:00 22/11/18, tomorrow at 21:00 it will show 25:00:00.
I have only found how to do a CountdownTimer, or just a simple timer.
You can get the current time when you start the timer with:
long timerStart = System.currentTimeMillis();
And then when you want to show what the timer is at calculate it by doing
long timePassed = System.currentTimeMillis() - timerStart;
And that will give you the number of milliseconds since you started the timer. And to format it the way you want you can pass it into this function:
public static String convertMillisToHMmSs(long millis) {
long seconds = millis / 1000
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60));
return String.format("%d:%02d:%02d", h,m,s);
}
Edit: As mentioned by the other answers, you will need to store the timerStart somewhere to keep track of it after the app is closed/reopened. I would recommend something like shared preferences you can look at this question to figure out how to do that
Unless you are willing to create an app that will run in background the whole time for few days (which would be highly unoptimized for an app of this complexity)
I think the best solution would be to store your start date (start timestamp) somewhere. Either in Room or in Shared Preferences and not to program your APP to increase or decrease your counter by one every second, than rather to calculate difference between start and current timestamp every second.
There are obviously a lot questions about performance but according to your question I guess that you are not concerned by this, and it will be a good practice for you to optimize this solution to be faster and more precise.
Agree above with Quinn however, somewhere you need to create a file that stores the current time. Otherwise every time app restarts the variable timerStart will reset.
So you need to create a file that stores the 'timerStart' so that every time you start, it updates from the value.
This question already has answers here:
How do I measure time elapsed in Java? [duplicate]
(15 answers)
Closed 5 years ago.
I'm working on an Android app and I have this problem.
For example, if I delete a file (operation A) and I receive a new file (operation B), I want to know how much time has passed between the two operations. I want it to work also if, in between, the user changes the date of the system, turns off the internet or restarts the device.
I know that exists SystemClock.elapsedRealtime() but its value restarts from zero if the user restarts the device.
I cannot use System.currentTimeMillis() because it changes if user changes the date.
I cannot get a date from the internet.
Thanks
Use System.currentTimeMillis(). It gets the time elapsed since the epoch (January 1st 1970).
You need a global var:
long start;
on the first action:
start = System.currentTimeMillis();
Since it's the time from the epoch, restarting the device isn't going to change it (i.e. System.nanoTime would be reset). However, as with most other methods, it isn't safe from changing the time of the device. If someone changes the time on the device back to the start of the epoch, you will experience some problems.
Note that there is no way to get the exact time since the event happened if the time is changed. I.e. if the user does operation A, waits a few hours, sets the clock back to a few hours ago, there's basically no offline ways you can check that. If you use a server, you can get the time from that, but there's not any way to get the accurate, unmodified time difference offline that's tamper proof (where tampering is changing the time).
TL;DR: System.currentTimeMillis is an offline option, but it isn't safe from time changing. If you need it to show the right time difference independently of the user changing the time of the device, use a server.
EDIT:
If you can't use System.currentTimeMillis or get a time from the internet, you can't measure the time at all. AFAIK, every Java/Android API relies on System.currentTimeMillis (or get the current time some other way). Example: the Date class can be converted to a Long representing the current time in milliseconds. For long-term timing, you either have to use System.currentTimeMillis or a server. System.nanoTime restarts when the JVM restarts. So does elapsedRealTime.
You just need to grab the time from somewhere before and after the activities you want to time and take one from the other. This uses the system clock but you could equally get the real time from some other source
long startTime = System.currentTimeMillis();
// Your code
System.out.println("Operation took " + (System.currentTimeMillis() - startTime) + " milliseconds");
iam trying to make a task to be executed EVERYDAY at 8am (example).
Currently what i have done is make a task, execute it automatically but only for a period of time (in this case 50 seconds) i want it to be executed every day at 8am.
my current code :
Timer time = new Timer();
certif_envia_notificacion st = new certif_envia_notificacion();
time.schedule(st, 0, 50000);
where certif_envia_notificacion is the task that i want to execute, and it does execute every 50 seconds.
Currently the only way i can figure this out is in the task certif_envia_notificacion ask for the time and execute if its 8am, but that seems like a huge waste of server resources, since ill have to execute the task at least 24 times a day.
Thanks in advance.
This is kind of a hack solution that I thought up, but feel free to give comments/feedback based on your requirements.
Basically, from my understanding you have a Java program that runs automatically when the computer starts up (if it restarts), and will then check every hour to see if it is the correct time to run the application. Why not keep it running this way, but instead of running every hour, calculate the time required to get to the next time required (e.g. 8PM the next day).
So for example, lets say you restart it at 3:15PM. When the java program runs, get it to get the current time (System.currentTimeMil...) and then calculate the time required to get to 8PM. You can do some simple math on this (e.g. 20 - current time = time required to wait) and if its negative, simply add 24 to it (e.g. if its 20 - 23, the answer is 24 - 3 = 21, thus wait 21 hours for the next 8PM).
This process can be used every time you run the code, or on startup. I'd recommend just creating a simple function to calculate the required sleep time.
What i finally did was execute the procedure every one hour and make a question of the hour and select the hour that i wanted, i know its not the best way but i needed to finish it fast....
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
if(hour == DesiredHour){
//do stuff
}else if(hour == DesiredHour2){
//do stuff 2
}
Thanks for the comments.
I have an Android app which finds the time interval between two events.
When Event 1 fires a broadcast, I store the current time using System.currentTimeInMillis()
I do the same when the second event occurs and then calculate the difference.
However, the result is always a couple of seconds off, in the sense that I know the interval was around 4 seconds but the value I get is around 6.
Is this because of the delay between sending the broadcast, receiving it and then storing the value?
If so, what's a better way to do it to get a more accurate value?
Per Developer.android.com (http://developer.android.com/reference/android/os/SystemClock.html):
System.currentTimeMillis() is the standard "wall" clock (time and
date) expressing milliseconds since the epoch. The wall clock can be
set by the user or the phone network (see setCurrentTimeMillis(long)),
so the time may jump backwards or forwards unpredictably. This clock
should only be used when correspondence with real-world dates and
times is important, such as in a calendar or alarm clock application.
Interval or elapsed time measurements should use a different clock.
If you are using System.currentTimeMillis(), consider listening to the
ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED
Intent broadcasts to find out when the time changes.
How can i synchronize the time (second , hour).
i have this
int minuto = cal.get(Calendar.MINUTE);
int day_Completed = 1440;
but i have no idea how can i do it.
I tried doing this
changing the pc time when a loop is running to see if the var minuto change.
but doesn't work.
An instance of calendar statically reflects one moment in time. It won't get updated automatically, it is not behaving like a clock.
If you want to "synchronize" with the actual time, then you have to
use some sort of timer, maybe based on Thread.sleep for a start
on each "timer event" get the current time (System.currentTimMillis()) and
update the instance of calendar with that value.