2017-08-11 72 views
-2

关于这个问题有很多问题,但是他们都没有解决我的问题。ProgressDialog没有关闭

我有一个活动,当点击一个按钮新的活动打开。一开始我会展示一个进度对话框。到目前为止没有错。在这个新的活动中,我试图建立一个蓝牙连接,并且需要很长时间。 (这是我使用进度对话框的原因)最后,我正在运行我创建的侦听器。 (onSucces & onFailed)侦听器运行时,意味着连接已完成,结果成功或失败。在这之后(连接完成后),我试图关闭进度对话框,问题是进度对话框没有关闭。这里是我的代码:

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

}

我怎样才能解决这个问题,或者我可以使用进度对话框的呢?我需要阻止用户访问接口,直到连接完成。

回答

0

使用您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(); 
} 
+0

isShowing()方法返回false。我把这样的东西:if(progressDialog.isShowing())Log.d(TAG,“PROGRESS DIALOG IS SHOWING”); progressDialog.dismiss();} else { Log.d(TAG,“PROGRESS DIALOG IS NOT SHOWING”); }在日志部分,我可以看到“PROGRESS DIALOG IS NOT SHOWING”。 – Burak

+0

试试这个我已经更新我的回答 –

+0

工作,谢谢。 – Burak

0

你应该打电话给你的服务中的AsyncTask内,并打电话给你progessdialog您onPostExecute内解散。

你有她的例子。 progressDialog in AsyncTask

+0

bluetoothConnectionService是我的Java类,它处理连接的东西。 – Burak

+0

我应该在活动中放置这个AsyncTask吗? – Burak

+0

是, 公共类AnaEkranActivity延伸AppCompatActivity { @覆盖 保护无效的onCreate(捆绑savedInstanceState){mserviceBth =新SeviceBth(任何信息);} 公共类SeviceBth延伸的AsyncTask <空隙,整型,布尔> {...} – Mehdiatique