2

I am writing a code in which I need to move all file and folders to the sdcard. I used Async task for this purpose. During this activity I am showing a progressbar with percentage on my screen instead of just showing me the "Loading..." popup. But it does not meet my requirement.

public class syncMgr extends AsyncTask<String, Long, String> {

public LoginActivity activity;
public Context context;
syncMgr(LoginActivity activity1,Context c)
{
    activity = activity1;
    context=c;
} 

//public ProgressDialog progress; 

   protected void onPreExecute() {
       super.onPreExecute();
       activity.progress = ProgressDialog.show(context,"","Files Downloading, Please Wait...",true);
    }

@Override
protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    copyFilesToSdCard();
    return null;
}

private void copyFilesToSdCard() {
    copyFileOrDir(""); 

}
private void copyFileOrDir(String path) {
    AssetManager assetManager = activity.getAssets();
    String assets[] = null;
    try {
        Log.i("tag", "copyFileOrDir() " + path);
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            String fullPath = TARGET_BASE_PATH + path;
            Log.i("tag", "path=" + fullPath);
            File dir = new File(fullPath);
            if (!dir.exists() && !path.startsWith("images")
                    && !path.startsWith("sounds")
                    && !path.startsWith("webkit"))
                if (!dir.mkdirs())
                    Log.i("tag", "could not create dir " + fullPath);
            for (int i = 0; i < assets.length; ++i) {
                publishProgress((int) ((i / (float) 658) * 100));
                String p;
                if (path.equals(""))
                    p = "";
                else
                    p = path + "/";

                if (!path.startsWith("images")
                        && !path.startsWith("sounds")
                        && !path.startsWith("webkit"))
                    copyFileOrDir(p + assets[i]);
            }
        }
    } catch (IOException ex) {
        Log.e("tag", "I/O Exception", ex);
    }
}
private void publishProgress(int i) {
    // TODO Auto-generated method stub
activity.progress.setProgress(i);
}
@Override
protected void onProgressUpdate(Long... values) {
    activity.progress.setProgress(values[0].intValue());
}

 @Override
    protected void onPostExecute(String result) {
     activity.progress.dismiss();
     super.onPostExecute(result);
        //return "asdas";
     //return result;
}       
}

Here is my Activity Class Code...

ProgressDialog progress;
public static final int progress_bar_type = 0; 
 /**
 * Showing Dialog
 * */
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case progress_bar_type: // we set this to 0
        progress = new ProgressDialog(this);
        progress.setMessage("Downloading file. Please wait...");
        progress.setIndeterminate(false);
        progress.setMax(100);
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.setCancelable(true);
        progress.show();
        return progress;
    default:
        return null;
    }
}
Zaid Iqbal
  • 1,662
  • 5
  • 25
  • 45

1 Answers1

0

Have you tried putting the asyncTask initiation code into a worker thread like this?

// set progressBar .VISIBLE first

// then...
new Thread(new Runnable() {
    public void run() {

        // webview initiation code

    }
}).start();

I turn on progressBar visibility beforehand and not in onPreExecute().

Here is how it solved my own problem & here are the docs.

Community
  • 1
  • 1
rockhammer
  • 957
  • 2
  • 13
  • 38