のプログレスバーを更新するのに少し問題が必要ですAsyncTask
。今のところ、これを使用してプログレスバーを更新しています:
cancelDialog.setMax(100);
while(myProgress<100) {
myProgress+=5;
publishProgress(myProgress);
}
問題は、インターネットに接続し、データパケットをダウンロードし、サイズに応じてプログレスバーを更新することです。プロセス全体は、サーバーに接続し、パケットをダウンロードし、それらを解析してデータベースに挿入する他のいくつかの機能によって実行されています。だから私がやりたいのは、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");
}
}
それで、どんなアイデアでも、どうすればこの問題を解決できますか?前もって感謝します!