There are many questions about this but none of them has a solution for my case.
I have an activity, when clicked a button new activity is opened. At the beginning I'm showing a progress dialog. There is nothing wrong so far. In this new activity I'm trying to make a Bluetooth connection and it takes long time. (This is the reason why I use progress dialog) At the end I'm running listeners that I created. (onSucces & onFailed) When listeners run, it means connection is done either with the result success or failed. After this point (after connection is done) I'm trying to close the progress dialog and the problem is progress dialog isn't closing. Here is my code:
public class AnaEkranActivity extends AppCompatActivity {
private static final String TAG = "AnaEkranActivity";
private static final String FEEDBACK_OK = "ok$";
private static final String FEEDBACK_ERR = "err$";
BluetoothConnectionService bluetoothConnectionService;
BluetoothAdapter bluetoothAdapter;
BluetoothDevice bluetoothDevice;
ProgressDialog progressDialog;
ButtonOnClickLstnr lstnr1;
ButtonOnClickLstnr lstnr2;
ButtonOnClickLstnr lstnr3;
ButtonOnClickLstnr lstnr4;
String address;
String tarihSaatString;
String acilDurdurString;
Button acilDurdurButton;
Button manuelButton;
Button ayarlarButton;
Button bioGuyAktivatorButton;
public static AnaEkranActivity instance;
Handler handler;
SendMessage sndmsg;
String readMessage;
Date nowDate;
DateFormat dateFormat;
DateFormat dateFormat2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ana_ekran);
Log.d(TAG, "is started.");
progressDialog = new ProgressDialog(AnaEkranActivity.this);
progressDialog.show(AnaEkranActivity.this, "Cihaza bağlanılıyor", "Lütfen bekleyiniz...");
instance = this;
handler = new Handler();
nowDate = new Date();
dateFormat = new SimpleDateFormat("yyyy/MM/dd");
dateFormat2 = new SimpleDateFormat("hh.mm.ss");
Intent intent = getIntent();
address = intent.getStringExtra("Device Address");
tarihSaatString = "tarih:" + dateFormat.format(nowDate) + ";saat:" + dateFormat2.format(nowDate) + "$";
acilDurdurString = "mode:stop$";
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothDevice = bluetoothAdapter.getRemoteDevice(address);
manuelButton = (Button) findViewById(R.id.manuelButton);
ayarlarButton = (Button) findViewById(R.id.ayarlarButton);
acilDurdurButton = (Button) findViewById(R.id.aciDurdurButton);
bioGuyAktivatorButton = (Button) findViewById(R.id.bioGuyAktivatorButton);
bluetoothConnectionService = BluetoothConnectionService.getInstanceBCS();
bluetoothConnectionService.setDevice(bluetoothDevice);
bluetoothConnectionService.startClient();
bluetoothConnectionService.setOnBluetoothConnectingListener(new BluetoothConnectionService.OnBluetoothConnectingListener() {
@Override
public void onSuccess() {
progressDialog.dismiss();
Log.d(TAG, "Connection is successfull.");
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(AnaEkranActivity.this, "Bağlantı başarıyla tamamlandı", Toast.LENGTH_SHORT).show();
}
});
bluetoothConnectionService.connected();
sndmsg = new SendMessage(bluetoothConnectionService, tarihSaatString);
sndmsg.sendMessage();
}
@Override
public void onFailure() {
progressDialog.dismiss();
Log.d(TAG, "Connection is failed.");
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(AnaEkranActivity.this, "Cihaza bağlanılamadı, lütfen bağlantılarınızı kontrol ederek tekrar deneyiniz.", Toast.LENGTH_SHORT).show();
}
});
startActivity(BaslangicActivity.class);
}
});
bluetoothConnectionService.setOnBluetoothConnectionListener(new BluetoothConnectionService.OnBluetoothConnectionListener() {
@Override
public void onConnectionLost() {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(AnaEkranActivity.this, "Bağlantı kesildi", Toast.LENGTH_SHORT).show();
}
});
startActivity(BaslangicActivity.class);
}
@Override
public void onRead() {
readMessage = bluetoothConnectionService.getIncomingMessage();
if (readMessage.equals(FEEDBACK_OK)) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(AnaEkranActivity.this, "İşleminiz başarıyla gerçekleştirildi.", Toast.LENGTH_SHORT).show();
}
});
} else if (readMessage.equals(FEEDBACK_ERR)) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(AnaEkranActivity.this, "İşlem başarısız, lütfen tekrar deneyiniz.", Toast.LENGTH_SHORT).show();
}
});
}
}
});
lstnr1 = new ButtonOnClickLstnr(bioGuyAktivatorButton);
lstnr1.openActivityOnClick(AnaEkranActivity.this, AktivatorActivity.class, address);
lstnr2 = new ButtonOnClickLstnr(manuelButton);
lstnr2.openActivityOnClick(AnaEkranActivity.this, ManuelActivity.class, address);
lstnr3 = new ButtonOnClickLstnr(ayarlarButton);
lstnr3.openActivityOnClick(AnaEkranActivity.this, AyarlarActivity.class, address);
lstnr4 = new ButtonOnClickLstnr(acilDurdurButton);
lstnr4.messageOnClick(bluetoothConnectionService, acilDurdurString);
}
public void startActivity(Class activity) {
Intent intent = new Intent(AnaEkranActivity.this, activity);
startActivity(intent);
}
}
How can I solve this problem, or what can I use instead of progress dialog? I need to prevent user access to interface until connection is done.