I need a little problem of updating my progress bar in AsyncTask. For now I'm using this to update my progressbar :
cancelDialog.setMax(100);
while(myProgress<100) {
myProgress+=5;
publishProgress(myProgress);
}
The problem is that I'm connecting to the internet, downloading data packets and depending on the size I want to update the progressBar. The whole process is being executed by a few other functions which are connecting to the server, downloading the packets, parse them and insert into databases. So the thing that I want to do is, to create something like a Timer which will read the number of downloaded packets on every second and depending on that will update the progress bar.
So here is how my AsyncTask looks like :
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");
}
}
So any ideas, how can I solve this issue? Thanks in advance!