Timer not working properly in android - android

Any one give idea, Timer will be schedule based on duration array and if once complete duration array length again will be start from first. In this some loop working perfectly some times will be crash and through arrayoutofboundsexception. Any one cane Help Me???
int[] duration={10000,2000,8000};
int layoutIncrment=-1;
private void layoutRotate()
{ layoutIncrment++;
Timer timer = new Timer();
timer.schedule(new TimerTask()
{ public void run()
{
if((duration.length-1) <= layoutIncrment)
layoutIncrment=-1;
layoutRotate();
Log.i("Rotation",String.valueOf(layoutIncrment));
} },duration[layoutIncrment],100000);
}

why dont u try like this. it will keep running in the while loop.. thats only u want. right?
int n=1;
while(n>0)
{
for(int i=0;i<duration.length;i++)
{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add ur code here
}
}, duration[i]);
}
}

Related

How to hold a for loop for some specific time of Period?

I want hold a for loop for specific time of period.
I was using Thread.Sleep(duration) But It is wrong.
Please let me know any solution for this Problem. In advance Thanks for suggestion. If there is any concern please let me know.
Try this:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//write here your code
}
}, 5000);
You can use Handler
for (int i = 0; i<50 ;i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// perform your task here
}
}, 1000 );// delay time in miniseonds
}
To hold for loot for a specific time : just copy past above snippet and try to change millisecond 1 second = 1000 millisecond
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
for (int i=0;i<50;i++){
tvOutput.setText(i+"\n");
}
}
},1000); //time in millisecond
int x= 5; // this condition is given as an example
for(int i=0 ; i<=50 ;i++){
if(x==5){ // this condition is given as an example
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// what you want to do
}
},1000);
}
}

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.

I need a simple incrementation over time function

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

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

Image view image need to change periodically in android

I have an image view. So in the image view i want to change the images after a certain time period. Images are coming from an array list. Now when the no of images are 3 or more than 3, it is working perfect. But when it is 2, my logic is not working. Second image is visible for a moment and then again changed to first image here is my code:
r = new Runnable(){
int i = 0;
public void run(){
iv.setImageBitmap(alBmps.get(i));
i++;
if(i >= alBmps.size()){
i = 0;
}
iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
}
};
iv.postDelayed(r, 1000);
Can any one help me what changes i need on the above code?
Thanks.
Try this
declare variables
static int i=0;
private Timer myTimer;
in your onCreate or on button click where you want to call and start the methods
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 500, 5000);
add these methods to your class
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
if(i<alBmps.size())
{
iv.setImageBitmap(alBmps.get(i));
}
else
{
i=0;
iv.setImageBitmap(alBmps.get(i));
}
i++;
}
};

Categories

Resources