-2

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.

2 Answers2

0

Use your progressDialog like that:

progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Message");
progressDialog.setTitle("Title");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();

after dismiss like this its working

if (progressDialog.isShowing()){
    progressDialog.dismiss();
    progressDialog.cancel();
}
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
  • isShowing() method returns false. I put something like this: if (progressDialog.isShowing()){ Log.d(TAG, "PROGRESS DIALOG IS SHOWING"); progressDialog.dismiss(); } else { Log.d(TAG, "PROGRESS DIALOG IS NOT SHOWING"); } In the log section, I can see "PROGRESS DIALOG IS NOT SHOWING". – Burak Özpoyraz Aug 11 '17 at 09:09
0

You should call your service inside an AsyncTask, and call your progessdialog dismiss inside your onPostExecute.

You have an example her. progressDialog in AsyncTask

  • bluetoothConnectionService is my java class which handles the connection stuff. – Burak Özpoyraz Aug 11 '17 at 09:12
  • Should I put this AsyncTask in the activity ? – Burak Özpoyraz Aug 11 '17 at 09:14
  • yes , public class AnaEkranActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { mserviceBth = new SeviceBth(any info);} public class SeviceBth extends AsyncTask { ... } – Mehdiatique Aug 11 '17 at 09:16
  • I have an error at the point int and boolean isn't accepted. It says "Type argument cannot be of primitive type". – Burak Özpoyraz Aug 11 '17 at 09:26
  • its just an exemple of my code, but you should use this link to make your asyncTask https://stackoverflow.com/questions/4538338/progressdialog-in-asynctask – Mehdiatique Aug 11 '17 at 09:48