Juliano Rodrigo Lamb
Juliano Rodrigo Lamb

Reputation: 126

AsyncTask example using loop

I'm implementing an example of AsyncTask and starting from the example here AsyncTask Android example.

However, I want to make a change. In my GUI I want to put a text box where the user can type something and, every 10 seconds, that name appears on the label.

This way, if the user has entered a new value, it will be displayed on the label, instead of the static text "Executed", which is now displayed.

My code is bellow.

XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:max="10"
            android:padding="10dip">
        </ProgressBar>
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Progress" >
        </Button>
        <TextView android:id="@+id/output"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Replace"/>

        <EditText
            android:id="@+id/textInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />
    </LinearLayout>

The MainActivity.java

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {

    Button btn;
    EditText textInput;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);
        textInput = findViewById(R.id.textInput);
        // because we implement OnClickListener we only have to pass "this"
        // (much easier)
        btn.setOnClickListener(this);
    }

    public void onClick(View view) {
        // detect the view that was "clicked"
        switch (view.getId()) {
            case R.id.button1:
                new LongOperation().execute("");
                break;
        }
    }

    private class LongOperation extends AsyncTask<String, Void, String> {

        String input;

        @Override
        protected String doInBackground(String... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                    input = textInput.getText().toString();
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
            }
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            TextView txt = (TextView) findViewById(R.id.output);
            txt.setText(input); // txt.setText(result);
            // might want to change "executed" for the returned string passed
            // into onPostExecute() but that is upto you
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }
}

Where will I put the code to listen? I want the label to change, checking every 10 seconds (for example) if there was a change in the text box ...

EDIT

Based on the answers, can I leave the infinite loop in this way? How do I stop this AsyncTask if I need to?

private class LongOperation extends AsyncTask<String, String, String> {

    String input;

    @Override
    protected String doInBackground(String... params) {
        while (true)
            try {
                Thread.sleep(1000);
                input = textInput.getText().toString();
                publishProgress(input);

            } catch (InterruptedException e) {
                Thread.interrupted();
                return "Executed";
            }
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(String... values) {
        TextView txt = (TextView) findViewById(R.id.output);
        txt.setText(values[0]);
        // update the UI with Data received from publishprogress
    }
}

Upvotes: 1

Views: 383

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

You can use publishprogess to update UI from background thread.

Change Void to String to pass string type and update UI in progressUpdate

private class LongOperation extends AsyncTask<String, String, String> {
//                                                    ^^^^^^
        String input;

        @Override
        protected String doInBackground(String... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                    input = textInput.getText().toString();
                    publishProgress(input);
                   //^^^^^^^^^ pass the data to update UI
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
            }
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            TextView txt = (TextView) findViewById(R.id.output);
            txt.setText(input); // txt.setText(result);
            // might want to change "executed" for the returned string passed
            // into onPostExecute() but that is upto you
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(String... values) {
            TextView txt = (TextView) findViewById(R.id.output);
            txt.setText(values[0]);
            // update the UI with Data received from publishprogress
        }
    }

Note: you can move textView initialisation in oncreate and declaration outside oncreate to avoid initialising it again and again, it's a costly process


Edit: To stop your loop you can use

while(!isCancelled()){ // run as long as task is not cancelled

}

To cancel the task, first store the reference

AsynchTask task = new LongOperation().execute("");
task.cancel(true);

Upvotes: 2

Related Questions