I need a simple incrementation over time function - android

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);

Related

Why isn't my text string updating?

So I am trying to display the time passed since pressing a button on my app.
My code is:
/*This will initiate the timer*/
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
start=System.currentTimeMillis();
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
The app runs but when I press the button it just shows "0.0".
The app doesn't close out. Any help is appreciated!
Please try this:
/*This will initiate the timer*/
timer = new Timer();
start=System.currentTimeMillis();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
In your code, the value of start is changed everytime the timer elapsed (start = System.currentTimeMillis()), so the value of time is always 0 (System.currentTimeMillis() - System.currentTimeMillis() should be 0 if it is called with no, or very small time difference...). So you should set the value of start on button press, then calculate the difference, and update text view in your timer task.

Increment an integer ever 30 seconds after button press Android

I'm looking for a way to increment an integer every 30 seconds after a button is pressed. The problem I am having is that my code currently waits 30 seconds to increment the integer by 1 but then only one second for every integer after. this is my code.
int delay = 5000; // delay for 5 sec.
int period = 30000; // repeat every 30 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(ammo_remaining<10){
update_ammo();``
}
}
});
}
}, delay, period);
Moving the recursive postdelayed call into your (ammo<10) if statement should give you the behavior you want.
int delay = 5000; // delay for 5 sec.
int period = 30000; // repeat every 30 sec.
int ammo_remaining = 10;
boolean reloading = false;
Handler handler=new Handler();
protected void shoot()
{
ammo_remaining--;
update_ammo();
if( ! reloading) //if not reloading
{
reloading = true;
handler.postDelayed(r, delay);
}
}
Runnable r=new Runnable()
{
public void run()
{
if(ammo_remaining<10)
{
ammo_remaining++;
update_ammo();
handler.postDelayed(r, period);
}
else{reloading=false;}
};
};
edit:
when shooting multiple times, you are creating many instance of the runnable through the handler, that all flood in after the first 30 seconds... you just need to create a boolean flag to keep track of the first request to reload
that 'reloading' flag and check should do the trick...
Button btn = (Button)findViewById(R.id.your button);
// first get your button;
final Handler hand = new Handler();
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
hand.post(new Runnable(){
#override public void run(){
(your int)++; // this will increase your int on step;
hand.postDelayed(this,30*1000); // this will do the same thing after 30 seconds again;
}
}
}
});
Try using handler:
int delay = 5000; // delay for 5 sec.
int period = 30000; // repeat every 30 sec.
Handler handler=new Handler();
protected void your_calling_method()
{
handler.postDelayed(r, delay);
}
Runnable r=new Runnable()
{
public void run()
{
if(ammo_remaining<10)
{
update_ammo();
}
handler.postDelayed(r, period);
};
};

How to run code in TimerTask regualarly

I use this code to run my code regularly ,
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//my code here
}
});
}
}, 0, 50);
There is a parameter of timer receives Date object to run code in specifec date, But
I need to Run my code with timertask every friday of week,
Any way?
Afakomoallah, Best regards.

Displaying a stopwatch in an android app in a textView

I have setup a stop watch using the com.apache.commons library and the stop watch seems to work fine. What I don't know how to do is display this stopwatch in a textView in my app. In general, I have no idea how that would work, i.e. How exactly would a stopwatch be displayed in a textView, given that the time on a stopwatch keeps changing constantly? At the moment, I have the code below and it updated the text in the textView every second for about 2 seconds and then I got a weird error. I'm not even sure if this is the right way to go about doing this. Please help!
Timer timer = new Timer();
TimerTask timerTask;
timerTask = new TimerTask() {
#Override
public void run() {
timeText.setText(time.toString());
}
};
timer.schedule(timerTask, 0, 1000);
The error I got after 2 seconds (and it successfully updated the time) was :
"only the original thread that created a view hierarchy can touch its views"
You can only update a TextView on the UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
//stuff that updates ui
}
});
Your code becomes
Timer timer = new Timer();
TimerTask timerTask;
timerTask = new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
timeText.setText(time.toString());
}
});
}
};
timer.schedule(timerTask, 0, 1000);
You may have to do myActivityObject.runOnUiThread() if you're getting an error there.
See this for more detail.
To update a view from another thread, you should use handler.
private void startTimerThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
private long startTime = System.currentTimeMillis();
public void run() {
//Change the condition for while loop depending on your program logic
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable(){
public void run() {
timeText.setText(time.toString());
}
});
}
}
};
new Thread(runnable).start();
}

how to change something in a new TimerTask action in android?

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...

Categories

Resources