0

我需要一个小问题来更新我的进度条AsyncTask。现在我正在使用它来更新我的进度条:

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

问题是我正在连接到互联网,下载数据包并根据我想要更新进度条的大小。整个过程由连接到服务器、下载数据包、解析数据包并插入数据库的其他一些函数执行。所以我想做的是,创建类似 a 的东西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");
    }
}

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

4

0 回答 0