I have an activity class as below.
public class LoginActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (ImageView) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
BackgroundRunner bgRunner = new BackgroundRunner(this);
String[] params = new String[]{url};
bgRunner.execute(params);
}
}
And the AsyncTask is:
public class BackgroundRunner extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
ServiceCaller serviceCaller = new ServiceCaller();
boolean status = serviceCaller.checkLogin(params[0]);
return status;
}
@Override
public void onPreExecute(){
progressBar = (ProgressBar) currentContext.findViewById(R.id.loader);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPostExecute(final Boolean status){
progressBar.setVisibility(View.INVISIBLE);
}
}
Here is the scenario. The main activity class creates a thread on a click. The then created thread fetches some data from the server. It is a time consuming task. So a progress bar is displayed on the UI. Currently I am using AsyncTask to accomplish server data retrieval. But the real challenge is wait for the background task to complete and get the value from it. What I am looking for is:
wait until server calls are made and get the results. Meanwhile show the progress bar. I think Handler would be an option. I am far less clear on that.
Any thoughts?
My solution was create my own asynctask class:
And this is the implementation: