如果有一个while循环,则对话框片段不会显示

时间:2017-11-03 12:47:51

标签: android dialog android-dialogfragment

我有一个控制器发出排球请求,我想阻止它继续使用代码,如果互联网连接不可用,因此,我设计了一个对话框片段来显示互联网连接是否不可用,以及一个检查的按钮连接,如果有连接,对话框将被解除。

这是我的代码:

if(!CheckInternetConnection.getInstance(context).isOnline()){
    NoInternetConnection noInternetConnection=new NoInternetConnection();
    noInternetConnection.setContext(context);
    noInternetConnection.show(fragmentManager,"NoInternetConnection");
    while (!CheckInternetConnection.getInstance(context).isOnline());
}

请注意,当我评论while循环时,会出现对话框,但是我需要它来检查连接

1 个答案:

答案 0 :(得分:0)

    new AsyncTask<Object, Object, Object>() {
        @Override
        protected Object doInBackground(Object[] objects) {
            while (!CheckInternetConnection.getInstance(context).isOnline()) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            /*
            Call some method in the DialogFragment to dismiss it.
             */
            noInternetConnection.dismissDialog();
        }
    }.execute();

您应该使用AysncTaskLoader而不是AsyncTask,因此此线程不会同时执行。最好在每个循环之间等待一段时间。我不确定你CheckInternetConnection到底发生了什么。干杯!