Is it possible to prioritize progressdialog
vs. Thread
?
In my app, after clicking a button, I need to freeze progressdialog
for 2 seconds. After that, I generate a query to my webservice
, and as soon as I return the data, they are presented in alertdialog
, which after opening, causes progressdialog
to receive dismiss()
to close it. However, even instantiating a new Thread
and setting sleep()
or wait()
, the process only freezes Thread
for integer and does not display progressdialog
. On the screen, first the alert is generated and the progress stays in the background until the alert is closed.
Is there a possible way to first generate Progress with 2 seconds of freeze and then the alert dialog? Here is the snippet of the code.
final EditText Nome = (EditText) findViewById(R.id.cmdDigDe);
Button btnConDeE = (Button) findViewById(R.id.btnConDe);
btnConDeE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ProgressDialog progressDialog = new ProgressDialog(DemE.this);
progressDialog.setTitle("Listando medicamentos");
progressDialog.setMessage("Buscando...");
progressDialog.show();
String nomeProduto = Nome.getText().toString();
String laboratorio = null;
String aliquota = "17";
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (nomeProduto == "") {
Toast.makeText(DemE.this, "Por favor digite o nome do medicamento", Toast.LENGTH_LONG).show();
} else
try {
URL url = new URL("http");
URLConnection con = url.openConnection();
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write("nome=" + nomeProduto + "&aliquota=" + aliquota + (laboratorio != null ? "&laboratorio=" + laboratorio : ""));
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result = "";
String line;
while ((line = reader.readLine()) != null) {
result += line;
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(Html.fromHtml(result));
final String text = tv.getText().toString();
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Medicamentos:");
alertDialog.setMessage(text);
alertDialog.setButton("Voltar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
progressDialog.dismiss();
alertDialog.show();
}
writer.close();
reader.close();
}