全局 AsyncTask 中未显示进度对话框 class

Progress dialog box not showing in global AsyncTask class

这是我的 MainActivity...我放了一个按钮,在按钮 onclick 中我调用了 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 没有显示(可能是 运行 背景)。

在我删除 prgDialog.dismiss();在 onPostExecute() 方法中,然后我的对话框在 doInBackground() 方法之后显示。

我想在 onPreExecute() 方法中显示对话框并在 onPostExecute() 方法中关闭。

这里:

...execute().get();

调用 get 方法将阻塞主 ui 线程,直到 doInBackground 执行未完成。这就是 ProgressDialog 没有显示的原因。

仅使用 execute() 方法启动 AsyncTask,在 doInBackground 方法执行期间显示进度对话框:

    new AllPrinterClass(AsyncMainActivity.this,
                     "123456789123456789", "123").execute();