update UI by calling AsyncTask objects many times - android

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

Related

How to limit user to use limite duration of service of my flutter android application? Lo

I am building an app for video editing...
I want to give some free minutes for 1st time using of app . User can edit the videos until the completion free minutes .
These minutes count based on duration of video which is editing..
After that they have to purchase for further any duration of videos..?
How can I limit user to use some until free minutes?
Till now I didn't used any online service in my app. Everything worked on device.
Any way to implement it on offline.?
If not possible..
What can I do to tackle it..?
I not a native English speaker..
Try to understand... :)
This is a timer that will trigger when no time is left.
It works completely offline .
This goes in the Class outside any other method where you usually declare your variables: (Note that you can set the time you want in millis. I chose 10000ms so 10 seconds as an example)
private CountDownTimer timer;
private long secondsLeft = 10000; // how much milliseconds of
// free time you want to have
//
And this goes where you want to start the time.
timer = new CountDownTimer(secondsLeft, 1000) {
public void onTick(long millisUntilFinished) {
secondsLeft = millisUntilFinished/1000;
}
public void onFinish() {
// The time is up! User has spent the time
}
}.start();
Then add these (or if you already have these methods then add the code in them to your ones.
#Override
protected void onPause() {
super.onPause();
timer.cancel();
}
#Override
protected void onResume() {
super.onResume();
timer = new CountDownTimer(secondsLeft, 1000) {
public void onTick(long millisUntilFinished) {
secondsLeft = millisUntilFinished/1000;
}
public void onFinish() {
// The time is up! The user has used the app for your amount of time
}
}.start();
}
Note: To make it survive an app restart or a phone restart you have to save "secondsLeft" to your device and load it when the app starts.

CountDownTimer Check every two seconds

I have one CountDownTimer and i want to do an action for every two wasted seconds.
countdowntimer = new CountDownTimer(10000, 1) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
}.start();
For example when it starts time remaining = 10 seconds
When time remaining = 8 seconds I want to do an action
When time remaining = 6 seconds I want to do an action
and so on......
Just check if it is divisible by 2.
#Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished/1000;
if ((seconds % 2) == 0)
{ // even number--do some action }
}
This is assuming you are calling the onTick() every second like
countdowntimer = new CountDownTimer(10000, 1000) {
This is probably preferable for you
Setting it up to call every 2 seconds should also work and better but leaving the original answer in case someone needs to call onTick() more often
countdowntimer = new CountDownTimer(10000, 2000) {
then you could just do the action with every call to onTick()
CountDownTimer Docs

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.

Sleep() in 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.

how to display Count down clock?

In my application one option is there which is used to select the application trigger time that is implemented in RadioButtons like : 10 mins, 20Mins, 30mins and 60mins
after selecting one of them the application starts at that time.. up to this working fine.
But, now I want to display a count down clock after selecting one option from above from that time onwords one clock, I want to display
That will useful to user how much time remaining to trigger the application
if any body knows about this please try to help me
Thanks for reading
Something like this for short counts:
public void timerCountDown(final int secondCount){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if (secondCount > 0){
//format time and display
counterBox.setText(Integer.toString((secondCount)));
timerCountDown(secondCount - 1);
}else{
//do after time is up
removeCounterViews();
}
}
}, 1000);
}
But this one is even better and it has two events which update the main UI thread: on tick and on finish, where tick's lenght is defined in the second parameter:
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Categories

Resources