I want to run a ProgressDialog(Spinner) when I send and receive some packets in a bluetooth device when a button is pressed. Here is the code
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Handler handler = new Handler();
Runnable r=new Runnable()
{
public void run()
{
dialog1 = ProgressDialog.show(Test.this, "",
"Receiving. Please wait...", true);
}
};
handler.post(r);
blueToothServer.getDevicebyMac();
blueToothServer.rfSocket();
blueToothServer.cancelDiscovery();
blueToothServer.connectSocket();
//toastMessage("Socket connected");
blueToothServer.ipstream();
blueToothServer.opStream();
blueToothServer.SendnRXDataToBTServer(2, "some String");
String Result1 = blueToothServer.response(1);
tv1.setText("Temperatue : " + Result1);
dialog1.dismiss();
}
But I’m not getting the ProgressDialog. But I can able to send and receive the packets. But when I comment the blueToothServer operations, I’m getting the progress dialog. Please explain me the situation where I can get both. Thank You.
You should be showing the
ProgressDialogin the UI thread and executing the bluetooth operations in a background thread. It is exactly for this purpose, theAsyncTaskis provided.Take a look at this answer: https://stackoverflow.com/a/3347405/1321873