Frage

I have an app that sends a file through a socket. While doing this I want to show the progress in a ProgressDialog. The app sends the file perfectly but I'm not able to make the dialog appear.

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 is the class that contains the socket to send the files.

I think the problem is due to I'm calling the thread inside the AssyncTask because when I make Thread.sleep(10000) I can see the ProgressDialog for that time, but I don't know how to fix it.

Also, when I run the debugger I can see that the variable 'counter' is incremented every time I call it, but if I add a watch with 'downloadDialog.getProgress()' the progress is always 0.

War es hilfreich?

Lösung

You are creating an AsyncTask, which doInBackground() method RUNS IN BACKGROUND. In there, you don't do anything, but start a new thread... Now this thread does the work, but your AsyncTask finishes, because it has nothing to do after starting the other thread... So, your ProgressDialog is shown for some milliseconds, then your AsyncTask finishes and the ProgressDialog is hidden again. But the thread that is doing the work is still running, only your AsyncTask has finished.

Solution : Either use an AsyncTask OR use a thread.

Andere Tipps

You need to call publishProgress on doinBackground()

Example:

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";
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top