Sleep() in android - android

I am working on a mini game for Android. I am using this code to create a 3 minute timer that pauses when the game is paused:
class update extends Thread{
#Override
public final void interrupt(){
super.interrupt();
}
#Override
public void run(){
try{
while(true){
sleep(50);
iTime++;
if(iTime>=3600)
bEnd = true; //finish
postInvalidate();
}
}catch(InterruptedException e){
}
}
}
update thread = null;
3600 = 20*3*60
The timer finishes in the emulator within about 5 minutes, and in the Galaxy Tab after about 1.5 minutes.
Why doesn't it always take 3 minutes? How do I ensure that it finishes within 3 minutes?

How about using the Android CountDownTimer class?
// 30 second countdown
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

I am not sure what are you doing in the postInvalidate() but maybe you can do something with AlarmManager and a service. You could create a task that will run in 3 minutes from now, and it will execute the service which, in your case, will invalidate the game.
I've seen many questions that were trying to handle timeouts with sleep method and I believe Android doesn't guaranteed that it will work that way.
Post back if you need help creating the service or running something with AlarmManager. I answered a question about it a couple of days ago.

Related

update UI by calling AsyncTask objects many times

I am making simple app , that display questions , the user within 10 seconds should answer or click next, when user click next , next question is displayed and the timer goes again to 10 seconds.
i am using Asytask to handle the time counter , but when i click next button , the next question is displayed but the timer delay like 2 seconds or so to start again from 10 ,
for example:
on the screen : question 1 is displayed and the time left is 8 seconds .
when i click next button
the question 2 is displayed but the time is 8 then after 2 or 3 seconds the time goes to 10 and start decreasing :
my question is :
is there a better way to handle this ? and why is when the next question is displayed it the time hang like 2 or 3 seconds then start again from 10
here is my code :
// this method is called to reset the timer to 10 and display next
question
private void displynextquestion(){
// cancel the current thread .
decrease_timer.cancel(true);
decrease_timer =new Decrease_timer();
// execute again and set the timer to 10 seconds
decrease_timer.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,10);
// some code
}
private class Decrease_timer extends AsyncTask <Integer ,Integer,Void>{
#Override
protected Void doInBackground(Integer... integers) {
for (int i=integers[0];i>=0;i--){
publishProgress(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
timeview.setText(""+values[0]);
}
}
}
Use CountDownTimer, is much more easy:
CountDownTimer countDownTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
};
The first argument in the CountDownTimer constructor is the total time in milliseconds, and the second argument is the interval along the way to receive onTick(long) callbacks.
To restart, just call:
countDownTimer.cancel();
countDownTimer.start();
See more in https://developer.android.com/reference/android/os/CountDownTimer.html

How to fetch data from DB in android every 2 mins?

I developed a simple android application and was able to post and get information from database through http request and doing this with AsyncTask. My question is if i want to poll information from the db every 5 mins or check if any new data has been posted to the db how i could this be done in android?
Use CountDownTimer
check this out
300000ms = 5 min
CountDownTimer chkDb= new CountDownTimer(300000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
//code for db check
chkDb.start();//start again after 5 min
}
}.start();
OR
Use TimerTask
private TimerTask mTimerTask;
private Timer t = new Timer();
public void doTimerTask() {
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
callYourMethodHere();
Log.i("TIMER", "TimerTask run");
}
});
}
};
// public void schedule (TimerTask task, long delay, long period)
t.schedule(mTimerTask, 500, 1000); //
}
public void stopTask() {
if (mTimerTask != null) {
Log.i("TIMER", "timer canceled");
mTimerTask.cancel();
}
}
task -- This is the task to be scheduled.
delay -- This is the delay in milliseconds before task is to be executed.
period -- This is the time in milliseconds between successive task executions.
I don't think that polling the DB every 5 min is a nice solution. It's a waste of resources.
Instead of that, consider using some mechanism that informs you when something in the DB has change so your UI can refresh properly.
For example, in some projects I have used Otto to send an event when a new item has been inserted in the DB. My fragments subscribe to a default bus so they are aware when something has change.
Pros: Performance. The app only works when it has to
Cons: Well, not much really. The easy solution is to say: "OK, something has change. Let's throw all the current info shown in UI and make a new query to DB!". BUT the nice solution is to say: "OK, something has change. What has change and how can I add that new info to UI?"
But that depends on the app context ;)

CountDownTimer skipping one second when displaying on TextView

I want to display a simple CountDownTimer in a TextView, but it seems to go from 30 down to 28 right away, as if there was a lag in between. I'm not sure how to go about on fixing this small bug. Here is my code:
This is in the click listener of the Button:
new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
There are 2 issues:
The first issue is that the countdown timer doesn't execute until time has elapsed for the first tick, in this case, after 1000ms.
The second is that the elapsed time is only approximate. millisUntilFinished is not guaranteed to come in increments of the interval (you can see that if you don't divide by 1000, the first tick is slightly under 29000).
Another thing to keep in mind is that you're not guaranteed to receive a tick call. That is to say, if the device did not have enough time to complete a tick, it may skip it (generally only noticeable for faster intervals).
To solve issue 1, you can simply run the onTick code (or refactor it into its own method) and run it as you start the countdown timer.
For issue 2, you can simply round the number.
For example:
new CountDownTimer(30000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
performTick(millisUntilFinished);
}
#Override
public void onFinish()
{
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
performTick(30000);
void performTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
}
If you want to make sure the appropriate value is updated with minimal latency, you may want to consider reducing the interval.
It is skipping because CountDownTimer is not a precise timer. Try to print just the millisUntilFinished without dividing it you'll see some excess number that it wont be exactly 29000 when it hits its first tick.
you can refer on this thread for the solution.

Multiple CountDownTimers with a uiversal Cancel command

Im back to ask another question :)
In an app I am making, I need a selector to choose one of 4 different countdown timers, for example round1, round2, etc.
but i have 2 problems with the same source. Using the default countdowntimer implentation which doesnt involve naming it round1 but running as a straight countdowntimer, I can cancel it in onPause using
coutdowntimer.cancel();
but after naming it round1 I can no longer pause it and the round1.cancel(); comes up as undefined.
I have spent most of yesterday and throught to 3am trying to find a solution and i thought i had it by making my timers final but it still didnt work.
after thinking about it more i relaised once ive solved this problem, I'm going to have to implement an if statement in my onpause which i would rather not do in an effort to keep my code clean.
So is there a way to implement a universal cancel command to cancel the root countdowntimer function regardless of the actual name its running under.
for example
public void canceltimers(){
//if round1,2,3,4 is runnng, cancel the main countdowntimer thread altogether.
countdowntimer.Cancel();
}
any help is appreciated and here is my current timer code.
CountDownTimer round1 = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
CountDownTimer round2 = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
// round1.start();
ive not implemented my if statements for the level select yet but will be similar to this
if (level == 1){
round1.start();}
if (level == 2){
round2.start();}
So far i have tried setting it as final, with no joy.
I have tried
round1.cancel();
with no joy. as well every implementation of countdowntimer i can think of or find.
Now as a request, im not as amazing as making sense of code as most of you guys so if you could try and give an example of the implementation it would be appreciated, Thanks for looking and thanks for your time & help
phil
make a single timer. Add multiple timertasks to this timer. Define the COUNTDOWN_TIME.
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
#Override
public void run() {
//execute code when the COUNTDOWN_TIME has ran out.
}
}, COUNTDOWN_TIME);
}
//cancels all sceduled/running TimerTasks
mTimer.cancel();

I'd like to make a countdown timer do a continuous loop in android - need opinions on how to do it

Is there a way to continuously loop through a countdown timer? I have a basic timer of going though 60 seconds, then updating a text field and it's working, but I want to add the functionality: when it's counted down, to automatically restart again until the user cancels it? Maybe run it through a thread? Not sure how to handle it. Here is what I have, and again, this code works, but I can only stop and start the countdown timer, not do a continuous loop:
cdt = new CountDownTimer(60000,1000) {
public void onTick(long millisUntilFinished) {
tvTimer.setText("remaining : " + millisUntilFinished/1000 + " secs");
}
public void onFinish() {
tvTimer.setText("");
bell.start();
}
};
/***************On Click/Touch Listeners*******************/
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tvTimer.setText("");
btnStart.setText("Start Timer");
SetImageView2(myDbHelper);
cdt.cancel();
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!TimerTicking){
cdt.start();
btnStart.setText("Stop Timer");
}
else {
tvTimer.setText("");
cdt.cancel();
btnStart.setText("Start Timer");
}
}
});
One very basic way to loop a CountDownTimer is to call start() in onFinished().
public void onFinish() {
...
start(); // Start this timer over
}
(Make sure you cancel your CountDownTimer in onPause() when you do this otherwise the timer might leak and keep firing in the background... Oops.)
However CountDownTimers has fundamental flaws (in my opinion): it often skips the last call to onTick() and it gains a few milliseconds each time onTick() is called... :( I re-wrote CountDownTimer in a previous question to be more accurate and call every tick.
The first value to the CountDownTimer() constructor, the total time to run, is a long and can hold a value approaching 300 million years. That ought to be long enough for most mobile applications. :-)
So just call it with cdt = new CountDownTimer(Long.MAX_VALUE, 1000) for a one second loop that will run continously.

Categories

Resources