4

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.

Anuj Zunjarrao
  • 426
  • 2
  • 14

1 Answers1

4

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();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Now its working but i want some return value like Integer RetuenIntValue = new AllPrinterClass(AsyncMainActivity.this, "123456789123456789", "123").execute().get(); thats why am use get(). now how can i get return??? – Anuj Zunjarrao Feb 05 '15 at 09:11
  • 1
    @AnujZunjarrao: Not possible ro return value. but you can use `onPostExecute` method for updating UI elements. or you can also create a [Android Custom Event Listener](http://stackoverflow.com/questions/22881661/android-custom-event-listener) to trigger event in AsyncMainActivity when `onPostExecute` method called – ρяσѕρєя K Feb 05 '15 at 09:14
  • @Override protected void onPostExecute(Integer result) { //this is where you will get the integer u need } – Sachin Rao Feb 05 '15 at 09:18
  • @SachinRao: OP want to get value in `AsyncMainActivity` Activity – ρяσѕρєя K Feb 05 '15 at 09:18
  • @ ρяσѕρєя K: ooh!, then he will need to write a callback method, i guess – Sachin Rao Feb 05 '15 at 09:21
  • this may help : http://stackoverflow.com/questions/6119305/android-how-to-run-asynctask-from-different-class-file – Sachin Rao Feb 05 '15 at 09:23
  • ya may be i will go with global variable for return values..... but tell me one think how can i w8 async task completed.... in onclick button i want close AsyncMainActivity is i get success value... – Anuj Zunjarrao Feb 05 '15 at 09:30