2014-11-21 73 views
0

我正在使用教程來創建一個下載類,它使用進度對話框。 show和dissmiss方法位於asynchTask類的受保護類中。 IDE告訴我它不能解決它們你能告訴我爲什麼這些方法超出範圍嗎?

public class DownloadHandler { 

    private Context mContext; 
    public String filename; 
    private String remotePath; 

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
    private ProgressDialog mProgressDialog; 

    public DownloadHandler(String rp, String f, Context c) throws Exception { 
    mContext = c; 
    remotePath = rp; 
    filename = f; 
} 

private void startDownload() { 
    String url = "http://example.com/"+remotePath+"/"+filename+".pdf"; 
    new DownloadFileAsync().execute(url); 
} 

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

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

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

//===================showDialog can not be resolved============================ 

     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 lengthOfFile = conexion.getContentLength(); 

      InputStream input = new BufferedInputStream(url.openStream()); 
      //write it to the internal storage 

      OutputStream output = new FileOutputStream(filename+".pdf"); 


      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       publishProgress(""+(int)((total*100)/lengthOfFile)); 

       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) {} 
     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 can not be resolved ================== 

     dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
//============================================================= 
    } 
} 

} 

它與受保護的類有什麼關係嗎?

+1

我沒有在[API文檔]中看到這些方法(http://developer.android.com/reference/android/os/AsyncTask .html) – 2014-11-21 00:33:40

+0

嗯,我不知道。這就是它在教程中的寫法。現在我做了一個沒有任何mod的直接複製和粘貼,我的IDE說這些方法已被棄用。 – silversunhunter 2014-11-21 00:34:11

+1

將我鏈接到教程 – 2014-11-21 00:35:14

回答

0

showDialogdismissDialogActivity類中。在教程中,它顯示類Download擴展了內部類可以使用的活動。

Download延伸Activity。至於不推薦使用的方法,這是因爲您應該使用DialogFragment。顯然,你正在遵循的教程是過時的,你應該看看how to use a DialogFragment

相關問題