1

So I come from the java world and normally when I want to loop I can just use something like:

while(true) {
    //code here till false
}

but in Android I am noticing that that kind of loop, at least in my onCreate() method causes an application to not be runnable. My loop I want to use is really short and calls some simple UI updates (changes the background color).

How can I implement a loop in Android that calls UI changes? Where do I put this loop? (clearly not onCreate) Examples/sample code would be much appreciated.

3
  • Do you need to make UI changes periodically? Commented Jun 7, 2011 at 17:38
  • if you are doing GUI stuff behind the scenes have you taken a look at Fragment class? developer.android.com/reference/android/app/Fragment.html Commented Jun 7, 2011 at 17:41
  • Not even behind the scenes, I just want it to start a loop that changes the background color and then not stop till a boolean is false Commented Jun 7, 2011 at 17:52

4 Answers 4

2

On average, how many times does this loop execute? If it is a loop used to continually monitor something, you should probably use a new thread to do that. You could use a singleton and synchronize access to a few getters/setters to pass data back and forth.

You probably already know this, but if you use a while(true) loop, you should also have a Thread.sleep(...) in there so as not to lock down the CPU.

You could do something like this:

public class SingletonClass implements Runnable {

    private boolean someValue;

    public static final SingletonClass INSTANCE = new Test();

    private Test() {
      Thread t = new Thread(this);
      t.setDaemon(true);
      t.start();
    }

    public synchronized boolean getValue() {
      return someValue;
    }

    public synchronized void setValue(boolean value) {
      someValue = value;
    }

    @Override
    public void run() {
      while (true) {
        // ... do your work here...

        // Monitor something and set values
        setValue(true);

        Thread.sleep(500);
    }
  }
}

In your activity you could access the necessary items like this:

SingletonClass.INSTANCE.getValue()
Sign up to request clarification or add additional context in comments.

Comments

2

You may want to rethink your design. You don't want to performing a blocking loop like that in the onCreate() method of your activity. You are preventing any UI updates and may end up just hanging your application. You will probably want to move the loop to an AsyncTask.

Comments

0

You also might try to look for an already existing method you can override/extend, like onDraw or onLayout. See the View documentation for further references.

Comments

0

I will recommend to use Handler and Runnable.

private Handler mHandler = new Handler();
private boolean isDone = false;
private Runnable indefinateTask = new Runnable() {
@Override
public void run() {
    if(isDone){
        mHandler.removeCallbacks(this);
    } else {
        //Perform Your Operation Over Here
        mHandler.postDelayed(indefinateTask, 1);
    }


}

};

and in onCreate call

new Thread(indefinateTask).start();

Might Help: http://developer.android.com/resources/articles/painless-threading.html

Hope this help. Cheers !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.