在Asynctask Android中使用百分比的进度条

时间:2014-06-16 09:50:57

标签: android android-asynctask android-progressbar

我正在编写一个代码,我需要将所有文件和文件夹移动到SD卡。我为此目的使用了异步任务。在此活动期间,我在屏幕上显示一个百分比的进度条,而不只是向我显示"正在加载..."弹出。但它不符合我的要求。

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;
}       
}

这是我的活动类代码......

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;
    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过将asyncTask启动代码放入这样的工作线程中?

// set progressBar .VISIBLE first

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

        // webview initiation code

    }
}).start();

我事先打开progressBar公开,而不是onPreExecute()

以下是how it solved my own problem&amp;这里是the docs