0

I followed the instructions given to me here (1. ASyncTask):

Download a file with Android, and showing the progress in a ProgressDialog

And it works to a point. The files download (using my own downloading code) and a spinning progress dialog shows up. Great. Only problem is that the dialog doesn't close when the code is done. That's probably because I can't get onPreExecute to work. I'm not quite certain where to put it. Anywhere the code goes it throws up a number of errors. Let me post what code I currently have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    c = this;
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    double getWidth = size.x * 0.9;
    scWidth = (int)getWidth;


    cWhite = getResources().getColor(R.color.white);
    cBlack = getResources().getColor(R.color.black);
    cTrans = getResources().getColor(R.color.transparent);  
    cBlue = getResources().getColor(R.color.blue);  
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    version = sharedPreferences.getString("version", "0.1");
    Editor editor = sharedPreferences.edit();
    editor.putString("area", "Home");

    editor.commit();
    setContentView(R.layout.activity_main);

    setupDrawer();
    if(sharedPreferences.getString("new", "Yes").equals("Yes")){
        loadFiles();
    }

private void loadFiles(){
    if(isNetworkAvailable() == true){


    ProgressDialog mProgressDialog;

    // instantiate it within the onCreate method
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("Downloading files...");
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    mProgressDialog.setCancelable(true);

    // execute this when the downloader must be fired
    final DownloadTask downloadTask = new DownloadTask(this);
    downloadTask.execute();
    mProgressDialog.show();
    mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            downloadTask.cancel(true);
        }

    });

    }else{
        new AlertDialog.Builder(this)
        .setTitle("No internet detected")
        .setMessage("Must have internet access to download files.")
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c);


                if(sharedPreferences.getString("new", "Yes").equals("Yes")){

                    ((Activity) c).finish();
                }
            }
         })


         .show();

    }
}

Then at the bottom in a new class I have:

private class DownloadTask extends AsyncTask<Integer, Void, Void> {

    private Context context;
    private PowerManager.WakeLock mWakeLock;

    public DownloadTask(Context context) {
        this.context = context;
    }

    @Override
    protected Void doInBackground(Integer... sUrl) {

            DataClass dc = new DataClass(c);
            //ArrayList<String> menuArray = dc.getMenuArray("Home");
            String[] menuArray = {"Home", "Announcements", "Speakers", "Events", "Exhibitors", "Agenda", "Hotels", "Documents"}; 
            File dir = getDir("myDir", Context.MODE_PRIVATE);

            for(int i=1; i <= 7; i++){
                String[] menuList = handleMenuInfo(menuArray[i]);
                for(int i2=1; i2 < menuList.length; i2++){
                File file = new File(dir, menuArray[i]  + "/" + menuList[i2] + ".txt");
                ArrayList<String> viewArray = dc.getInfoArray(menuArray[i],menuList[i2]);
                writeToFile(file, viewArray);
                }
            }
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c);
            Editor editor = sharedPreferences.edit();
            editor.putString("new", "No");
            editor.commit();

       return null;

    }
}


}

So where can I put the code to handle all of the other stuff, like progress and\or closing the dialog when it is over?

Community
  • 1
  • 1
Marcel Marino
  • 962
  • 3
  • 17
  • 34

1 Answers1

0

move the code for the progress bar inside your download task class using OnPreExecute

@Override
protected void onPreExecute() {
....progressbar code
}

then close it on onPostExecute with a dismiss

  @Override
 protected void onPostExecute() {
    mProgressDialog.dismiss();

}

BradR
  • 593
  • 5
  • 16