简体   繁体   中英

Progress dialog box not showing in global AsyncTask class

This is my MainActivity... i put one button, in button onclick i call 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();
    };
}

}

This is My 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();

}

}

This code working for me but my ProgressDialog not showing(May be run background).

In i remove prgDialog.dismiss(); in onPostExecute() Method then my dialog showing after doInBackground() Method.

I want dialog show in onPreExecute() Method and dismiss in onPostExecute() method.

Here:

...execute().get();

Calling get method will block main ui Thread until doInBackground execution not completed. that's why ProgressDialog is not showing.

Use only execute() method to start AsyncTask which show Progress Dialog during doInBackground method execution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM