0

I am using a tutorial to create a download class and it uses a progress dialog. The show and dissmiss methods are in protected classes inside of the asynchTask class. The IDE is telling me that it can not resolve them

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);
//=============================================================
    }
}

}

Does it have something to do with the protected class?

silversunhunter
  • 1,219
  • 2
  • 12
  • 32

1 Answers1

0

showDialog and dismissDialog are in the Activity class. In the tutorial, it shows that the class Download extends activity, which the inner class can then use.

Have Download extend Activity. As for the methods being deprecated, this is because you should be using a DialogFragment. Apparently, the tutorial you are following is outdated, and you should look into how to use a DialogFragment

Community
  • 1
  • 1
Vince
  • 14,470
  • 7
  • 39
  • 84