这是我的MainActivity ...我放了一个按钮,在按钮上点击我调用asyncTask
package com.example.asnytaskpro;
import java.util.concurrent.ExecutionException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AsyncMainActivity extends Activity implements OnClickListener {
Button button ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_async, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.err.println("1");
try {
new AllPrinterClass(AsyncMainActivity.this, "123456789123456789", "123").execute().get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}
这是我的AsyncTaskClass
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.widget.Toast;
public class AllPrinterClass extends AsyncTask<Integer, Integer, Integer>{
public static String ServerData = "";
private ProgressDialog prgDialog;
private Context context;
String TransNo = "" ,IMEINo = "";
int iRetValue = 0;
String CurrentDate = null, CurrentTime= null;
SimpleDateFormat df,tf;
public AllPrinterClass(Context context1, String imeiNo,String transno2)
{
this.context = context1;
this.IMEINo = imeiNo;
this.TransNo = transno2;
Calendar c = Calendar.getInstance();
df = new SimpleDateFormat("dd/M/yyyy");
CurrentDate = df.format(c.getTime());
tf = new SimpleDateFormat("hh:mm:ss");
CurrentTime = tf.format(c.getTime());
System.err.println(CurrentDate);
System.err.println(CurrentTime);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
this.prgDialog = new ProgressDialog(context);
this.prgDialog.setMessage("Place your finger on FPS ...");
this.prgDialog.setIndeterminate(true);
this.prgDialog.setCancelable(false);
this.prgDialog.show();
}
@Override
protected Integer doInBackground(Integer... params) {
//////////////////Doing my Code///////////////////
for (int i = 0; i < 10; i++) {
System.err.println("i :-" + i);
SystemClock.sleep(500);
}
/////////////////////////////////////////////////////
return iRetValue;
}
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
prgDialog.dismiss();
Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show();
}
}
这段代码对我有用,但我的ProgressDialog没有显示(可能会运行后台)。
在i中删除prgDialog.dismiss();在onPostExecute()方法然后我的对话框显示在doInBackground()方法之后。
我想在onPreExecute()方法中显示对话框,并在onPostExecute()方法中解除。
答案 0 :(得分:4)
下面:
...execute().get();
调用get方法将阻止主ui线程直到doInBackground
执行未完成。这就是为什么ProgressDialog
没有显示的原因。
仅使用execute()
方法启动AsyncTask
,在doInBackground
方法执行期间显示进度对话框:
new AllPrinterClass(AsyncMainActivity.this,
"123456789123456789", "123").execute();