I am trying to create my own implementation of android Asynctask. For that I created an abstract class which extends Thread class. I declared methods for onPreExecute, onPostExecute, onProgressUpdate, and doInBackground.
I am running the doInBackground method by creating a handler from Looper of main thread. But I am unable to modify UI elements inside my onProgressUpdate() method but I am able to modify UI elements in onPostExecute() method.
Here is what I tried.
package com.example.myapplication;
import android.os.Handler;
import android.os.Looper;
public abstract class MyAsyncTask extends Thread {
String[] urls;
Handler handler;
abstract protected void onPreExecute();
abstract protected void onPostExecute(String result);
abstract protected void onProgressUpdate(String result);
protected void publishProgress(String progress) {
onProgressUpdate(progress);
}
abstract protected void doInBackground(String... urls);
protected void execute(String... urls) {
onPreExecute();
this.urls = urls;
start();
}
public void run() {
try {
handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
doInBackground(urls);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is my MainActivity. It has progress bar and progressText to show progress.
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ProgressBar bar;
TextView progressText;
LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = findViewById(R.id.progress);
progressText = findViewById(R.id.progressText);
container = findViewById(R.id.container);
}
public void startProcess(View view) {
String[] assignments = {"assgn1", "assgn2", "assgn3", "assgn4", "assgn5"};
new ProgressAsync().execute(assignments);
}
public void showProgress() {
container.setVisibility(View.VISIBLE);
}
public void hideProgress() {
container.setVisibility(View.GONE);
}
public class ProgressAsync extends MyAsyncTask {
@Override
protected void onPreExecute() {
showProgress();
}
@Override
protected void onPostExecute(String result) {
progressText.setText("All processed");
hideProgress();
}
@Override
protected void onProgressUpdate(String result) {
progressText.setText(result + " processed");
}
@Override
protected void doInBackground(String... urls) {
for (int i = 0; i < urls.length; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(this.urls[i]);
}
onPostExecute("SUCCESS");
}
}
}
It supposed to set progressText like assgn1, assgn2 etc after every 2 seconds. But it doesn't set any text for progressText during onProgressUpdate(), but sets "All Processed" in onPostExecute() method.
Can someone help me if I am missing something here.
MyAsyncTask extends Thread. Thread is not AsyncTask. You needMyAsyncTask extends AsyncTask<String, String, String>- anatoli