i have a problem with the timerTask in android i have a code like this:
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
textview1.setText(countInt);
}
}, 1000);
every time the timer task get startet my app crashed, i thing because i'm accessing the textview and it is in a other thread right?
how to solve this?
Yes, you are right, it crashes cause' you are accessing views from not an UI thread. To solve this, you can post a Runnable to UI thread using your activity
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
textview1.setText(countInt);
}
});
}
}, 1000);
try this..
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
yourActivity.this.runOnUiThread(new Runnable()
public void run(){
{textview1.setText(String.valueOf(countInt))});
}
}
}, 1000);
It crashes because you are messing with something ( textview1.setText(countInt);) that belongs UI thread which is not allowed...
Related
i had tried this:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
videoview.getCurrentPosition;
}
},0,1000);
but it does not work(it crashes).
it says:
"cant create handler inside thread that has not called looper.prepare" =/
Please , post your answer if you have one , i found this way.
final Handler handler = new Handler();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
handler.post( new Runneable(){
public void run (){
//Your function =D
}
});
}
},0,1000);
I want to do a cookie clicker like app and i need a simple incrementation over time function.
But i would only want the int to start increasing once i have pressed a button.
I tried this but does not work properly.
int delay = 5000;
int period = 1000;
int count = 0;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
count++;
score.setText(String.valueOf(count));
}
}, delay, period);
The reason its not working is because run() is running on separate Thread, not on UIThread. You need to run setText in UIThread. see the code below
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
count++;
score.setText(String.valueOf(count));
}
});
}
}, delay, period);
I have a timer and timer task and handler and runnable
my code be execute every 10 seconds until ServerResponse variable in not empty and then redirect to another activity.
but when my code redirect to another activity timer task is working !!!!!
how can to stop timer task when we are in another activity??
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
if(timer!= null) {
timer.cancel();
timer.purge();
timer= null;
}
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);
This might not answer your question directly, but just reactive extensions are a nice alternative to using TimerTasks. Check this and this out.
subscription = Observable.interval(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())// Runs on a io thread pool
.observeOn(AndroidSchedulers.mainThread()) // Observes on UI thread
.subscribe(timeCount ->{
// Your code here
});
subscription.unsubscribe();// Stops the stream.
Rx with timer looks like the way to go. If you are not up for it Handler could work as well.
http://reactivex.io/documentation/operators/timer.html
You can try using :
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
public void playBeep(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
repeatBeep();
}
});
}};
t.schedule(scanTask, 10000, 10000);
}
public void repeatBeep(){
mp.start();
}
and call t.cancel() when you want to stop the beep
I would like the Timer to start when I turn ToggleButton ON and to cancel when I turn it OFF. It works, but when I try to start it over again after being cancelled I get an error. Where is the problem? Here is the code:
final ToggleButton btnLive = (ToggleButton)findViewById(R.id.live);
btnLive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (btnLive.isChecked()) {
timer = new Timer();
timer.schedule(timerTask, 5*1000, 5*1000);
} else {
timer.cancel();
timer.purge();
timer = null;
}
}
});
EDIT: It works with calling timer that way:
timer.schedule(new TimerTask(){
private Runnable runnable = new Runnable() {
public void run() {
new Something().execute();
}
};
public void run() {
handler.post(runnable);
}
}, 5*1000, 5*1000);
Can you explain me why first method is not working? Is it range problem?
Use this type of code and check.
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask()
{
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
}
});
}
}, 5000, 500);