I have an app that does the progress bar update, just like I'm doing in another app, but in this new app, onProgressUpdate()
only runs at the end of doInBackground()
.
The parseInsertSolicitantes(jsonString)
method is called, where in the record insertion loop, it has publishProgress(count)
.
Class Sincronizar
public class Sincronizar extends AppCompatActivity {
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sincronizar);
this.pb = findViewById(R.id.progressBarSync);
}
private void syncCadastros() {
AsyncCadastros asyncCadastros = new AsyncCadastros(this, this.pb);
String retorno = "";
try {
asyncCadastros.execute();
retorno = asyncCadastros.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.i("lgg", "retorno sync cad: " + retorno);
}
}
Class AsyncCadastros
public class AsyncCadastros extends AsyncTask<String, Integer, String> {
Context ctx;
ProgressBar pb;
ConnectivityManager conexao;
public AsyncCadastros(Context ctx, ProgressBar pb) {
this.ctx = ctx;
this.pb = pb;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i("lgg", "onPreExecute");
this.pb.setProgress(0);
this.pb.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("lgg", "onPostExecute");
this.progressBar.setVisibility(View.GONE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("lgg", "onProgressUpdate: " + values[0]);
this.pb.setProgress(values[0]);
}
@Override
protected String doInBackground(String... params) {
if (!verificaConexao(this.ctx)) {
return "Sem conexão";
} else {
...
String retorno = json;
if (retorno.equals("X")) {
return "X";
} else {
reader.close();
streamReader.close();
String jsonString = retorno.toString();
parseInsertSolicitantes(jsonString);
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (ProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
private boolean verificaConexao(Context ctx) {
...
}
private String parseInsertSolicitantes(String json) {
String jsonString = json;
JSONObject jsonObject;
JSONArray jsonArray;
int handle;
String nome;
DBController dbc = new DBController(ctx);
try {
jsonObject = new JSONObject(jsonString);
jsonArray = jsonObject.getJSONArray("solicitantes");
int count = 0;
int regs = jsonArray.length();
this.pb.setMax(regs);
Log.i("lgg", "registros: " + regs);
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
handle = JO.getInt("handle");
nome = JO.getString("nome");
dbc.atualizarSolicitantes(handle, nome);
Log.i("lgg", "Sync: " + count);
count++;
publishProgress(count);
}
return "parseInsertSolicitantes OK";
} catch (JSONException e) {
e.printStackTrace();
return "parseInsertSolicitantes ERRO";
}
}
}
Output example:
02-20 12:15:50.028 21648-21648/com.exemple.exemple I/lgg: onPreExecute
02-20 12:15:50.090 21648-7989/com.exemple.exemple I/lgg: conectado
02-20 12:15:50.363 21648-7989/com.exemple.exemple I/lgg: Solicitantes apagados
02-20 12:15:50.753 21648-7989/com.exemple.exemple I/lgg: registros: 5
02-20 12:15:50.785 21648-7989/com.exemple.exemple I/lgg: Cadastro inserido
02-20 12:15:50.786 21648-7989/com.exemple.exemple I/lgg: Sync: 0
02-20 12:15:50.825 21648-7989/com.exemple.exemple I/lgg: Cadastro inserido
02-20 12:15:50.826 21648-7989/com.exemple.exemple I/lgg: Sync: 1
02-20 12:15:50.866 21648-7989/com.exemple.exemple I/lgg: Cadastro inserido
02-20 12:15:50.867 21648-7989/com.exemple.exemple I/lgg: Sync: 2
02-20 12:15:50.899 21648-7989/com.exemple.exemple I/lgg: Cadastro inserido
02-20 12:15:50.900 21648-7989/com.exemple.exemple I/lgg: Sync: 3
02-20 12:15:50.931 21648-7989/com.exemple.exemple I/lgg: Cadastro inserido
02-20 12:15:50.932 21648-7989/com.exemple.exemple I/lgg: Sync: 4
02-20 12:15:50.943 21648-21648/com.exemple.exemple I/Choreographer: Skipped 56 frames! The application may be doing too much work on its main thread.
02-20 12:15:50.957 21648-21648/com.exemple.exemple I/lgg: onProgressUpdate: 1
02-20 12:15:50.959 21648-21648/com.exemple.exemple I/lgg: onProgressUpdate: 2
02-20 12:15:50.960 21648-21648/com.exemple.exemple I/lgg: onProgressUpdate: 3
02-20 12:15:50.961 21648-21648/com.exemple.exemple I/lgg: onProgressUpdate: 4
02-20 12:15:50.962 21648-21648/com.exemple.exemple I/lgg: onProgressUpdate: 5
02-20 12:15:50.962 21648-21648/com.exemple.exemple I/lgg: onPostExecute
An example of an app that is working:
public class JsonReceber extends AsyncTask<Void, Integer, String> {
Context ctx;
String tipoPessoa;
ProgressBar pb;
ConnectivityManager conexao;
public JsonReceber(Context ctx, String tipoPessoa, ProgressBar pb) {
this.ctx = ctx;
this.tipoPessoa = tipoPessoa;
this.pb = pb;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i("lgg", "Inicio da sync");
this.pb.setProgress(0);
this.pb.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
this.pb.setVisibility(View.GONE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("lgg", "progress: " + values[0]);
this.pb.setProgress(values[0]);
}
@Override
protected String doInBackground(Void... params) {
if (!verificaConexao(this.ctx)) {
return "Sem conexão";
} else {
StringBuilder content = new StringBuilder();
try {
URL url = new URL("http://");
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
Log.i("lgg", "erro: " + e);
}
Log.i("lgg", "recebido: " + content);
if (content.length() < 1) {
Log.i("lgg", "content vazio");
return "content vazio";
} else {
// Parse JSON e insert
String jsonString = content.toString();
sync(jsonString);
return null;
}
}
}
public boolean verificaConexao(Context ctx) {
this.conexao = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = this.conexao.getActiveNetworkInfo();
if ((netInfo != null) && (netInfo.isConnectedOrConnecting()) && (netInfo.isAvailable())) {
Log.i("lgg", "conectado");
return true;
} else {
Log.i("lgg", "sem conexão");
return false;
}
}
private void sync(String json) {
String jsonString = json;
JSONObject jsonObject;
JSONArray jsonArray;
int handle;
String nome;
DB_Controller dbc = new DB_Controller(ctx);
if (this.tipoPessoa.equals("solicitantes"))
dbc.apagarSolicitantes();
else if (this.tipoPessoa.equals("clientes"))
dbc.apagarClientes();
try {
jsonObject = new JSONObject(jsonString);
jsonArray = jsonObject.getJSONArray(this.tipoPessoa);
int count = 0;
int regs = jsonArray.length();
this.pb.setMax(regs);
Log.i("lgg", "registros: " + regs);
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
handle = JO.getInt("handle");
nome = JO.getString("nome");
if (this.tipoPessoa.equals("solicitantes"))
dbc.atualizarSolicitantes(handle, nome);
else if (this.tipoPessoa.equals("clientes"))
dbc.atualizarClientes(handle, nome);
Log.i("lgg", "Sync: " + count);
count++;
publishProgress(count);
}
} catch (JSONException e) {
e.printStackTrace();
}
}