2012-12-21 124 views
1

我創建了一個AlertDialog以讓用戶選擇是否從服務器下載某些東西。Android ProgressDialog不顯示

如果用戶選擇下載,則AlertDialog被解僱,並且出現連接到AsyncTaskProgressDialog

ProgressDialog從不顯示。 AlertDialog的「ok」按鈕保持選中狀態,直至AsyncTask的操作結束。

如果我在代碼中「評論」AsyncTask操作,AlertDialog被正確解除,並且ProgressDialog顯示出來。

我還沒有嘗試在真實設備上的應用程序,但只有模擬器。

這是問題所在?

+7

顯示部分的代碼。 – alex

+0

你有沒有得到這個解決?如果是這樣,請將適當的答案標記爲正確或添加您自己的答案並將其標記爲已接受。 – jnthnjns

回答

1

試試這個代碼,這可能使用AlertDialog肯定按鈕幫助您在AsyncTask例如

private class allinfo extends AsyncTask<String, Void, JSONObject> { 
     private ProgressDialog dialog; 

     protected void onPreExecute(){ 
      dialog = ProgressDialog.show(collection.this, "Loading", "Loading. Please wait...", true,false); 
     } 

     @Override 
     protected JSONObject doInBackground(String... arg0) { 
      // TODO Auto-generated method stub 

      return json; 
     } 
     protected void onPostExecute(JSONObject json) 
     { 
      dialog.dismiss(); 
      info(json); 
     } 

    } 
0

ProgressDialog實現:

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_layout); 

     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
     // set title 
     alertDialogBuilder.setTitle("Your Title"); 
     // set dialog message 
     alertDialogBuilder 
     .setMessage("Click yes to go to AsyncTask!") 
     .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       new MyAsyncTaskClass().execute(); 
      } 
     }) 
     .setNegativeButton("No",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       // if this button is clicked, just close 
       // the dialog box and do nothing 
       dialog.cancel(); 
      } 
     }); 
     // create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
     // show it 
     alertDialog.show(); 

    } 

    // Here is the start of the AsyncTask  
    class MyAsyncTaskClass extends AsyncTask<String, String, Void> { 

     private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); 

     protected void onPreExecute() { 
      progressDialog.setMessage("Dialog Message"); 
      progressDialog.show(); 
      progressDialog.setCanceledOnTouchOutside(false); 
     } 

     @Override 
     protected Void doInBackground(String... params) { 
      // TODO stuff 
     } 

     protected void onPostExecute(Void v) { 
      this.progressDialog.dismiss(); 
     } 
    } 
} 
1
final AlertDialog d = new AlertDialog.Builder(youclassname.this) 
     .setView(input) 
     .setTitle(R.string.message) 
     .setPositiveButton(android.R.string.ok, 
       new Dialog.OnClickListener() { 
        public void onClick(DialogInterface d, int which) { 
         //Do nothing here. We override the onclick 
        } 
       }) 
     .setNegativeButton(android.R.string.cancel, null) 
     .create(); 

d.setOnShowListener(new DialogInterface.OnShowListener() { 

    public void onShow(DialogInterface dialog) { 

     Button b = d.getButton(AlertDialog.BUTTON_POSITIVE); 
     b.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 

         startDownload();//begin downloading 
        d.dismiss(); 

      } 
     }); 
    } 
}); 
d.show(); 

這裏是startDownload一部分。

private void startDownload() { 
     String url ="file download link"; 
     Toast.makeText(dwn.this, url,Toast.LENGTH_LONG).show(); 
     new DownloadFileAsync().execute(url); 
    } 

這裏是的AsyncTask

class DownloadFileAsync extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

    @Override 
    protected String doInBackground(String... aurl) { 
     int count; 

    try { 

    URL url = new URL(aurl[0]); 
    URLConnection conexion = url.openConnection(); 
    conexion.connect(); 

    int lenghtOfFile = conexion.getContentLength(); 
    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

    InputStream input = new BufferedInputStream(url.openStream()); 
    OutputStream output = new FileOutputStream("/sdcard/file.type"); 

    byte data[] = new byte[1024]; 

    long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      publishProgress(""+(int)((total*100)/lenghtOfFile)); 
      output.write(data, 0, count); 
     } 

     output.flush(); 
     output.close(); 
     input.close(); 






      } 
      catch (Exception e) { 
       // TODO Auto-generated catch block 

      } 

    return null; 

    } 
    protected void onProgressUpdate(String... progress) { 
     Log.d("ANDRO_ASYNC",progress[0]); 
     mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    @Override 
    protected void onPostExecute(String unused) { 
     dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 
} 

這裏是進度對話框

@Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Downloading PDF file"); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
      return mProgressDialog; 
     default: 
      return null; 
     } 
    }