调用AsyncTask中的线程时,ProgressDialog不会出现

时间:2014-04-24 09:33:20

标签: android multithreading sockets android-asynctask progressdialog

我有一个通过套接字发送文件的应用。在这样做时,我想在ProgressDialog中显示进度。该应用程序完美地发送文件,但我无法显示对话框。

public class ProgressDialogActivity extends Activity {

private ProgressDialog downloadDialog = null;
private String filePath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    filePath = getIntent().getExtras().getString("filePath");

    downloadDialog = new ProgressDialog(this);
    Task myTask = new Task();
    myTask.execute(0);
}

private void showMessage(final String msg) {
    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), msg,  `enter code here`Toast.LENGTH_SHORT).show();
        }
    });
}


private class Task extends AsyncTask<Integer, Integer, Boolean> implements Observer
{
    private Thread t;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        downloadDialog.setTitle("SENDING");
        downloadDialog.setMessage("................");
        downloadDialog.setCancelable(false);
        downloadDialog.setIndeterminate(false);
//          downloadDialog.setMax(100);
        downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        downloadDialog.show();
    }

    @Override
    protected Boolean doInBackground(Integer... params) {
        SendFile send = new SendFile(filePath);
        downloadDialog.setMax(0);
        t = new Thread(send);
        send.registerObserver(this);
//          try {
//              Thread.sleep(10000);
//          } catch (InterruptedException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
        t.start();
        return true;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);

        int counter = values[0].intValue();
        downloadDialog.setProgress(counter);
        if(filePath != null)
        {
            downloadDialog.setMessage(filePath+"...");
        }
    }

    @Override
    public void update(Subject subject) {
        // TODO Auto-generated method stub
        if(subject instanceof SendFile)
        {
            SendFile e = (SendFile) subject;
            if(e.getException() != null)
            {
                t.interrupt();
                showMessage(e.getException());
            } else
            {
                if(!e.isStarted())
                {
                    initializeProgressBar(e.getNumIter());
                } else
                {
                    refreshProgressBar(e.getNumIter());
                }

                if(e.isSent())
                {
                    t.interrupt();
                    showMessage("File sent");
                }
            }
        }
    }

    public void initializeProgressBar(int max){
        downloadDialog.setMax(max);
    }
    public void refreshProgressBar(int amount){
        publishProgress(downloadDialog.getMax()-amount);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        if(downloadDialog != null)
        {
            downloadDialog.dismiss();
        }
                    finish();
    }

    @Override
    protected void onCancelled() {
        // TODO Auto-generated method stub
        super.onCancelled();
        t.interrupt();
        showMessage("TASK CANCELLED");
    }
};
}

SendFile是包含发送文件的套接字的类。

我认为问题是由于我在AssyncTask中调用线程,因为当我创建Thread.sleep(10000)时,我可以看到ProgressDialog,但我不知道如何修复它。 / p>

另外,当我运行调试器时,我可以看到每次调用它时变量'counter'都会递增,但是如果我添加一个带'downloadDialog.getProgress()'的监视器的进度始终为0

2 个答案:

答案 0 :(得分:0)

您需要在publishProgress

上致电doinBackground()

示例:

protected String doInBackground(Void... params) {
        try {
            int i = 0;
            Log.i("Thread","1");
            Thread.sleep(1000);
            publishProgress(i++);
            Log.i("Thread","2");
            Thread.sleep(1000);
            publishProgress(i++);
            Log.i("Thread","3");
            Thread.sleep(1000);
            Log.i("Thread","4");
            Thread.sleep(1000);
            Log.i("Thread","5");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "done";
    }

答案 1 :(得分:0)

您正在创建一个AsyncTask,其中doInBackground()方法RUNS IN BACKGROUND。在那里,你没有做任何事情,但是开始一个新的线程......现在这个线程完成了工作,但你的AsyncTask完成了,因为它在启动另一个线程后无事可做......所以,你的ProgressDialog显示几毫秒,然后AsyncTask完成,再次隐藏ProgressDialog。但正在进行工作的线程仍在运行,只有你的AsyncTask已经完成。

解决方案:使用AsyncTask或使用线程。