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);
//=============================================================
}
}
}
它與受保護的類有什麼關係嗎?
我沒有在[API文檔]中看到這些方法(http://developer.android.com/reference/android/os/AsyncTask .html) – 2014-11-21 00:33:40
嗯,我不知道。這就是它在教程中的寫法。現在我做了一個沒有任何mod的直接複製和粘貼,我的IDE說這些方法已被棄用。 – silversunhunter 2014-11-21 00:34:11
將我鏈接到教程 – 2014-11-21 00:35:14