Reputation: 4607
I need to display progrees dialog. It is quite simple, however I am absolutly required to wait untill asynctask will finish. That exopse some difficulties to me. To wait for completion of asynctask method .get()
is used. But this method blocks
my progress dialog.
So, how can I unblock progressdialog
with get or what can replace .get()
?
I am calling operation in a following way:
Operation o = new Operation();
o.execute(params);
try {
o.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
class Operation
class Operation extends AsyncTask<String, Void, JSONObject> {
ProgressDialog pd;
Context con;
@Override
protected void onPreExecute() {
pd = new ProgressDialog(app);
pd.setOwnerActivity(app);
pd.setTitle("Идет загрузка...");
pd.setCancelable(true);
pd.show();
}
@Override
protected JSONObject doInBackground(String... option) {
/*
some job
*/
return json;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pd.dismiss();
/*
some job
*/
}
}
Upvotes: 0
Views: 73
Reputation: 2348
You might want to change your code to something like this. Wrap the code you want to run afterwards in a runnable and pass it to your async task.
Runnable onEnd = new Runnable(){
//code to run after the task has completed
}
Operation o = new Operation(onEnd);
o.execute(params);
And here is the modified Operation class.
class Operation extends AsyncTask<String, Void, JSONObject> {
ProgressDialog pd;
Context con;
Runnable afterwards = null;
Operation(Runnable afterwards){
this.afterwards = afterwards ;
}
@Override
protected void onPreExecute() {
pd = new ProgressDialog(app);
pd.setOwnerActivity(app);
pd.setTitle("Идет загрузка...");
pd.setCancelable(true);
pd.show();
}
@Override
protected JSONObject doInBackground(String... option) {
/*
some job
*/
return json;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pd.dismiss();
/*
some job
*/
if (afterwards != null)
afterwards.run();
}
}
Note the afterwards.run()
in onPostExecute
.
Upvotes: 1
Reputation: 7092
try to use this way
private ProgressDialog dialog=null;
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(this, "", "Please! Wait...",true);
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
dialog.dismiss();
// use here code
}
Upvotes: 1
Reputation: 44571
Don't use .get()
as you have seen it blocks the UI
. You can use getStatus()
if you need to check if the task is finished. Removing .get()
should allow your ProgressDialog
to show. If you have code that must wait until the AsyncTask
is finished then put it in onPostExecute()
or call a function from there
If you need to reference something in the MainActivity
from the AsyncTask
after it has finished, simply create a constructor to take a reference of your Activity
and pass context
to your constructor
Operation o = new Operation(this);
and reference it in constructor
lass Operation extends AsyncTask<String, Void, JSONObject> {
ProgressDialog pd;
Context con;
Activity act;
public Operator(Activity curAct)
{
act = curAct; // can use this reference to access methods in your Activity
}
Upvotes: 1