9
It is possible to prioritize a progressdialog
before a Thread
?
In my app, after clicking a button, I need to freeze a progressdialog
for 2 seconds. After that, generate a query to my webservice
, and as soon as return the data, they are presented in a alertdialog
,that after opened, makes the progressdialog
receive the dismiss()
to close it. However, even instantiating a new Thread
and setting the sleep()
or wait()
, the process just freezes the Thread
for whole and does not present the progressdialog
. On the screen, first the Alert is generated and the Progress is in the background until the Alert is closed.
Is there a possible way to generate Progress first with 2 seconds of Freeze and then the Alert dialog? Follows excerpt from 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();
}
Diego, I don’t understand where you’re wearing
Thread
in this code. Also useStrictMode.ThreadPolicy.Builder().permitAll()
does not start a newThread
and is a bad practice because it masks some problems. This code is running onMainThread
and consequently locking the interface, I recommend taking a look at this question to resolve this: http://answall.com/questions/33509/como-usar-a-biblioteca-ksoap2/33514#33514.– Wakim
Wakim, I actually ended up posting the old code, sorry, if I instantiate a new thread, is there a way to put the progressidialog on hold to run on screen and freeze the 2 seconds? Or put the alertdialog itself on hold, for example. the code executes the progressidialog and displays on screen, then instantiates a new thread, puts this thread on hold for 2 seconds, and then executes the alertdialog showing the information returned by the URL. There is a way to adapt the code to run this way?
– diego figueiredo
Yes it is possible, but I recommend another approach. Display a
DialogFragment
(usingcancelable
asfalse
) and start aAsyncTask
in theonViewCreated
, updating it at the end of processing in the methodonPostExecute
.– Wakim
I will test here now! Thank you very much.
– diego figueiredo
You can try to perform the data search process in another thread, for example only when the thread has finished its execution would you call the demiss of the alertDialog... although maybe it is not the best solution..
– samuel emidio