ProgressDialog 没有关闭

ProgressDialog is not closing

有很多关于此的问题,但其中 none 个对我的情况有解决方案。

我有一个 activity,当单击一个按钮时,新的 activity 会打开。一开始我展示了一个进度对话框。到目前为止没有任何问题。在这个新 activity 中,我正在尝试建立蓝牙连接,但需要很长时间。 (这就是我使用进度对话框的原因)最后,我创建了 运行 宁听众。 (onSucces & onFailed) 当 listeners 运行 时,表示连接完成,结果为成功或失败。在这一点之后(连接完成后)我试图关闭进度对话框,问题是进度对话框没有关闭。这是我的代码:

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);
}

}

我该如何解决这个问题,或者我可以使用什么来代替进度对话框?在连接完成之前,我需要阻止用户访问界面。

像这样使用你的 progressDialog:

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

像这样解除后它的工作

if (progressDialog.isShowing()){
    progressDialog.dismiss();
    progressDialog.cancel();
}

您应该在 AsyncTask 中调用您的服务,并在您的 onPostExecute 中调用您的 progessdialog dismiss。

你有她的例子。 progressDialog in AsyncTask