소켓을 통해 파일을 보내는 앱이 있습니다. 이 과정을 진행하면서 ProgressDialog에서 진행 상황을 보여주고 싶습니다. 앱에서 파일을 완벽하게 전송하지만 대화 상자를 표시 할 수 없습니다.AsyncTask 내에서 스레드를 호출 할 때 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은 파일을 보낼 소켓이 들어있는 클래스입니다.
Thread.Sleep (10000)을 만들 때 그 시간 동안 ProgressDialog를 볼 수 있기 때문에 문제가 AssyncTask 내부의 스레드를 호출하기 때문에 발생한다고 생각합니다. 그러나 문제를 해결하는 방법을 모르겠습니다. 또한
, 내가 디버거를 실행할 때 변수 '카운터'내가 그것을 호출 할 때마다 증가되는 것을 볼 수 있습니다,하지만 난 'downloadDialog.getProgress()' 진행으로 시계를 추가하는 경우 은 항상 0입니다.
확실히 당신이 생각하기에 doInBackground()는 이미 스레드이므로 다른 스레드를 호출 할 필요가 없습니다! – Saqib
아마 표시되지만 너무 빨리 사라져서 보지 못합니다. 'doInBackground'는 이미 쓰레드이므로 다른 쓰레드를 호출 할 필요가 없다. –