Android从asyncTask中的另一个函数更新Pr​​ogressBar

时间:2011-12-08 15:31:18

标签: android android-asynctask android-progressbar

我需要在AsyncTask更新我的进度条时出现一些问题。现在我用它来更新我的进度条:

cancelDialog.setMax(100);
while(myProgress<100) {
    myProgress+=5;
    publishProgress(myProgress);
}

问题是我正在连接到互联网,下载数据包并根据我想要更新progressBar的大小。整个过程由一些其他功能执行,这些功能连接到服务器,下载数据包,解析它们并插入数据库。所以我想要做的是,创建像Timer这样的东西,它将读取每秒下载的数据包的数量,并根据它将更新progress bar

以下是我的AsyncTask的样子:

public class SyncWithHash extends AsyncTask<Context, Integer, Void> {

    @Override
    protected Void doInBackground(Context... arrContext) {
        try {
            // updating the progressBar
            cancelDialog.setMax(100);
            while (myProgress < 100) {
                myProgress += 5;
                publishProgress(myProgress);
            }
            String charset = "UTF-8";
            hash = getAuthHash();
            SharedPreferences lastUser = PreferenceManager
                    .getDefaultSharedPreferences(Synchronization.this);
            int userId = lastUser.getInt("lastUser", 1);
            systemDbHelper = new SystemDatabaseHelper(Synchronization.this,
                    null, 1);
            systemDbHelper.initialize(Synchronization.this);
            String sql = "SELECT dbTimestamp FROM users WHERE objectId="
                + userId;
            Cursor cursor = systemDbHelper.executeSQLQuery(sql);
            if (cursor.getCount() < 0) {
                cursor.close();
            } else if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                timeStamp = cursor.getString(cursor
                        .getColumnIndex("dbTimestamp"));
                Log.d("", "timeStamp : " + timeStamp);
            }
            String query = String.format("debug_data=%s&"
                + "client_auth_hash=%s&" + "timestamp=%s&"
                + "client_api_ver=%s&" + "set_locale=%s&"
                + "device_os_type=%s&" + "device_sync_type=%s&"
                + "device_identification_string=%s&"
                + "device_identificator=%s&" + "device_resolution=%s",
                URLEncoder.encode("1", charset),
                URLEncoder.encode(hash, charset),
                URLEncoder.encode(timeStamp, charset),
                URLEncoder.encode(clientApiVersion, charset),
                URLEncoder.encode(locale, charset),
                URLEncoder.encode(version, charset),
                URLEncoder.encode("14", charset),
                URLEncoder.encode(version, charset),
                URLEncoder.encode(deviceId, charset),
                URLEncoder.encode(resolution, charset));
            SharedPreferences useSSLConnection = PreferenceManager
                    .getDefaultSharedPreferences(Synchronization.this);
            boolean useSSl = useSSLConnection.getBoolean("UseSSl", true);
            if (useSSl) {
                UseHttpsConnection(url, charset, query);
            } else {
                UseHttpConnection(url, charset, query);
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        cancelDialog.setProgress(progress[0]);
    }

    @Override
    protected void onCancelled() {
        Log.d("", "ON CANCELLED");
    }

    @Override
    protected void onPreExecute() {
        Log.d("", "ON PRE EXECUTE");
        myProgress = 0;
    }

    @Override
    protected void onPostExecute(Void v) {
        Log.d("", "ON POST EXECUTE");
    }
}

所以任何想法,我该如何解决这个问题? 提前谢谢!

0 个答案:

没有答案