storing the count down timer value in android - java

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

Related

'setExact' method of AlarmManager not behaving correctly

I'm working on a CountDown app. I want to launch a 'end of countdown activity' when the countdown reaches 0. I have a variable keeping track of how much time is left (variable that I use to display the countdown).
The documentation gives me this (I have to use this method because it's part of the exercise):
public void setExact (int type,long triggerAtMillis,PendingIntent operation)
Knowing this I pass my countdown variable as the second argument thinking the activity would roughly be opened around the time I've given.
Let's say, I put a duration of 30s in the duration variable. I start the countdown by pushing a button and I use the 'setExact' method using the duration variable.
Even though, I pass 30s (obviously converted in millis), the 'end of countdown activity' launches only after 5s.
Feel free to download the project.
It sounds like you might be passing in 30000 for the trigger time, when it should probably be 30000 plus the current time in ms.
From the documentation:
triggerAtMillis long: time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type).

Know if uptime timer is reset or Android has been rebooted

I have a independent clock created in my application. The clock runs as a different thread in the activity, starting from a base time set by me. I update the clock using the difference between the uptimemillis when I set the clock, and the current uptimemillis. But the uptimetimer, can be reset by Android, and is ever reset when Android reboot.
I only want to know if the uptime timer is reset, to know if the clock is still reliable.
How?
According to the documentation you can use SystemClock.elapsedRealtime()
elapsedRealtime():
Returns milliseconds since boot, including time spent in sleep.
This value will only be reset when the device is restarted. Listen to the broadcast boot_complete and you will know when that is.
The problem with the updateMillis() is clearly noted in the documentation:
uptimeMillis():
Returns milliseconds since boot, not counting time spent in deep sleep. Note: This value may get reset occasionally (before it would otherwise wrap around).
From how I understand the documentation, by using elapsedRealtime your users cannot manipulate your counter.

Best class to schedule a task

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.

Create application session in android

My application require timeout of 10 min i.e. if there is no activity on the application from last 10 min, then either the application is terminated or login(Login credential in sqlite db) is shown.
I came up with the approach of using thread with maintain perform the task but that approach is untidy approach.
Suggest me some better approach.
I would do this way.
Save the current time (in millisec) to pref file in onPause();
Retrieve the time from pref file and compare with current time in onResume();
If the time difference is more than 10, just finish() the activity and ask for log in.
Use timer class to do this task / alternatively can use Alarm class for the same.
Depends on requirement.
Thread will certainly not be a good option.
You may use AsyncTask to do this job.

How do i get service timer for my video recording application?

Hi guys i wanted a "service" timer which enable my camera to show the elapsed(incremental) time of a timer when the video recording process has started in such a format 00:00:00? But i do not know how to code the above function in a service activity since it poses the problem of not allowing xml content to link with my mainActivity which in my case is a video recording activity to update the elapsed time on a textview, correct me if i'm wrong or is there a work around/solution?
And when the camera exits and returns the service should still keep track of the time since started and update the textview accordingly... Can someone help me on this matter i'm rather new to android/java programming?
This is what i have tried out so far for my mainActivity which performs the recording and display the updated elapsed time of the timer..
There may be a better answer, but you could just obtain the time when the recording starts and remember it. It's not clear what owns the textview, but whatever does could update it from the current time by subtracting the start time. You can also pass the service a callback handler which it could use to send you update messages.

Categories

Resources