您可以在活动中使用 AsynTask,只需将此类放在活动的同一文件中
=> 在 onPostExecute() 方法中,您将测试您的处理是否完成以关闭您的活动。
class MyAsynTask extends AsyncTask<Void, Integer, Boolean> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// Init your variables, in my case it's my Progress Bar
myprogress = new ProgressDialog(mcontext);
myprogress.setTitle("Update");
myprogress.setMessage("Update running ....");
myprogress.show();
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
boolean test = false;
int j = 0;
for (int i = 0; i < 50; i++) {
j = i + 10;
publishProgress(i);
// Your implemention code to connect to the server
if (i == 49)
test = true;
}
return test;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
myprogress.setProgress(values[0]);
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (myprogress.isShowing())
myprogress.dismiss();
if (result){
Toast.makeText(mcontext, "I Finished",Toast.LENGTH_SHORT).show();
yourActivity.this.finish();
}
}
}