0
I have an app that updates the progress bar, the same way I’m doing in another app, but in this new app, the onProgressUpdate()
only executes at the end of doInBackground()
.
It is called the method parseInsertSolicitantes(jsonString)
, where in the record insertion loop, it has the 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();
}
}
You must call
onProgressUpdate
within thedoInBackground
– Valdeir Psr
I didn’t know there was such a thing. But I wanted to do like the other, which is pretty much the same thing. I asked the question about the code of what is working.
– rbz
The other calls the method
publishProgress
insidesync(String json)
, which in turn is called by thedoInBackground
. That is, it is necessary to callonProgressUpdate
from the methoddoInBackground
– Valdeir Psr
The content of the method
sync
was inside thedoInBackground
, I just took it out to test, as I did in the new App. And it worked both ways. I’m not yet understanding why of putting one inside the other...– rbz
Even, the 2 call methods. The only difference is the
void
andString
in them.– rbz
You don’t mean the
publishProgress
instead ofonProgressUpdate
nay ?– rbz
Friend, looking closely your code seems to be all running, you say call the progressiUpdate at the end to update your progressionBar, but you have already put a log on inside the onProgressUpdate? Because I think everything is running so fast that the update is almost instantaneous.
– Weslley Barbosa
@Weslleybarbosa yes, the log is inside each part, is the "Example of output"... is not speed, because in the other app is faster still, including I limit to 5, 10, results for the test, because it has thousands. I haven’t taken to see it again yet, but it’s a mystery !
– rbz