2

I'm trying to learn Android programming and so far so good. Right now I'm trying to learn something about threads but i cant get my code to work.

I already read

http://developer.android.com/guide/faq/commontasks.html#threading http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#concurrency_handler

I tried to use both to make my code work but i have no idea how to do so since none of them fit my needs and both use the same example of a progress bar.

I have 3 buttons in my layout that i want to move (slowly) up or down. I wanted to do this with a thread since the mainUI activity should not stop working while doing this. So my idea was to start a new thread where i wait for a little time and than set the parameters, and i do this again and again.

I probably could solve it with global variables, but i dont want to use such a "cheap" trick for something like this.

My code looks like this (Most of it copied from the two sides since i cant find a working example for a thread in which parameters are given to Methods)

public class MainActivity extends Activity {

 // Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        updateResultsInUi();
    }
};

protected void moveMenu(int direction, final int time) {

    // Fire off a thread to do some work that we shouldn't do directly in the UI thread
    Thread t = new Thread() {
        public void run() {
            for (int i = 0; i < 10; i++) {
                doSomethingExpensive(time);
                mHandler.post(mUpdateResults);   // <--- Here im looking for a way to give i to the updateResultsInUi() Method
            }
        }
    };
    t.start();
}

private void updateResultsInUi(int i) {

    // Back in the UI thread -- update our UI elements based on the data in mResults
    ScrollView topScrollView = (ScrollView) findViewById(R.id.scrollView1);
    LinearLayout botLinearLayout = (LinearLayout) findViewById(R.id.LinearLayoutButton);

    LinearLayout.LayoutParams paramsScrollView = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0,90+i);
    LinearLayout.LayoutParams paramsLinearLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0,10-i);
    topScrollView.setLayoutParams(paramsScrollView);
    botLinearLayout.setLayoutParams(paramsLinearLayout);
}

// Simulating something timeconsuming
private void doSomethingExpensive(int time) {
  try {
    Thread.sleep(time);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}

public void button_click_del(View v)
{
    [...]
    moveMenu(direction, time);
}

[...]
}

2 Answers 2

3

If you want to pass values to the thread. The approach I follow is:

Create a new class that will extend the runnable class.

public interface myRunnable extends Runnable {

  public void setParams(int param1, int param2);
 }

now implement this class

public class ImplementMyRunnable implements myRunnable{
int param1;
int param2;
  public void setParams(int param1, int param2){
     this.param1 = param1;
     this.param2 = param2;
  }

 public void run(){
    //your code here and use as many parameters you want
 }
}

now in your moveMenu function

protected void moveMenu(int direction, final int time) {
   ImplementMyRunnable object1 = new ImplementMyRunnable();
   object1.setParams(1,12);
   Thread myThread = new Thread(object1);
   myThread.start();
}

basically what i wanted to point here is. Create a new class that extends the Runnable class and add what ever functionality you want now or you may require in future. Pass as many parameters as you want in constructor or in any other setter function.

This is a more neat approach and will help you to extend your code more easily and cleanly in future.

The code syntax might be wrong. I only wanted to share the approach. Hope it will help.

1
  • 1
    with some modifications I made it work on all of my Runnables. Thanks for the code
    – Desolator
    Aug 29, 2017 at 16:52
1

Create a new runnable on the go:

    Thread t = new Thread() {
        public void run() {
            for (int i = 0; i < 10; i++) {
                doSomethingExpensive(time);
                final int finalI = i;
                mHandler.post(new Runnable() {
                    public void run() {
                        updateResultsInUi(finalI);
                    }
                });
            }
        }
    };
    t.start();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.