小编典典
像下面这样
class GetStuffAsyncly extends AsyncTask {
String dialogString;
ProgressDialog dialog;
Context context;
AsyncListener listener;
// my vars....
public GetStuffAsyncly(String dialogMessage, Context con, AsyncListener listener) {
this.dialog = new ProgressDialog(con);
this.dialogString = dialogMessage;
this.context = con;
this.listener = listener;
}
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
listener.onTaskStarted();
}
@Override
protected String doInBackground(String... args) {
// do stuff in background...
return data;
}
/**
* After completing background task Dismiss the progress dialog
**/
protected void onPostExecute(String jsonString) {
// dismiss the dialog after getting all data
dialog.dismiss();
listener.onTaskFinished(jsonString);
}
}
和听众课
public interface AsyncListener {
void onTaskStarted();
void onTaskFinished(String data);
}
你可以这样打电话
new GetStuffAsyncly(message, this, new AsyncListener() {
@Override
public void onTaskStarted() {
//do your stuff
}
@Override
public void onTaskFinished(String data) {
//Do your stuff;
}
}).execute(parameter);
2020-11-01