Kotlin 方法完成后显示ProgressDialog

Kotlin 方法完成后显示ProgressDialog,kotlin,Kotlin,我试图在应用程序在网络中查找IP地址时显示ProgressDialog。在我目前的代码中,即使ProgressDialog的初始化在开始时,它也会在我等待的完成之后显示出来 这是我的密码: val clickListener = View.OnClickListener { view -> when(view.id) { R.id.button_upload -> { progressDialog = Prog

我试图在应用程序在网络中查找IP地址时显示ProgressDialog。在我目前的代码中,即使ProgressDialog的初始化在开始时,它也会在我等待的完成之后显示出来

这是我的密码:

 val clickListener = View.OnClickListener { view ->
        when(view.id) {
            R.id.button_upload -> {
                progressDialog = ProgressDialog(activity)
                progressDialog!!.setMessage("Looking for the server. Please wait...")
                progressDialog!!.setCancelable(false)
                progressDialog!!.show()
                if(findServer()) {
                   Log.i("TAG", "FOUND")
                } else {
                   Log.i("TAG", "NOT FOUND")
                }
            }
        }
    } 

    private fun findServer(): Boolean {
        if(canPingServer()) {
            Toast.makeText(context, "We are connected to the server server", Toast.LENGTH_LONG).show()
            gView.button_upload.setText("Upload")
            gView.button_upload.isEnabled = true
            progressDialog!!.dismiss()
            return true
        } else {
            Toast.makeText(context, "We cannot connect to the server.", Toast.LENGTH_LONG).show()
            gView.button_upload.setText("Server not found")
            gView.button_upload.isEnabled = false
            progressDialog!!.dismiss()
            return false
        }
    }

private fun canPingServer(): Boolean {
        val runtime = Runtime.getRuntime()
        try {
            val mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 192.168.1.4")
            val mExitValue = mIpAddrProcess.waitFor()
            Log.i("TAG","mExitValue $mExitValue")
            return mExitValue == 0
        } catch (ignore: InterruptedException) {
            ignore.printStackTrace()
            Log.i("TAG"," Exception:$ignore")
        } catch (e: IOException) {
            e.printStackTrace()
            Log.i("TAG"," Exception:$e")
        }
        return false
    }
我相信我必须为此创建
AsyncTask
,但问题是,这个片段已经从另一个类继承了

class UploadFragment : BaseFragment() {.....}

之所以显示它,是因为findServer()函数需要在不同的线程上执行

 val clickListener = View.OnClickListener { view ->
    when(view.id) {
        R.id.button_upload -> {
            progressDialog = ProgressDialog(activity)
            progressDialog!!.setMessage("Looking for the server. Please wait...")
            progressDialog!!.setCancelable(false)
            progressDialog!!.show()
            Thread(Runnable {
                if(findServer()) {
                   Log.i("TAG", "FOUND")
                } else {
                   Log.i("TAG", "NOT FOUND")
                }
            }).start()
        }
    }
} 
AsyncTask是java中实现多线程的另一种方法,但我相信上面介绍的方法会更好地满足您的需要。不过,您需要小心,因为任何必须在主线程上运行的操作(例如,祝酒词或设置元素文本的位置)仍然需要在主线程上进行。您可以通过围绕需要在主线程上运行的任何内容来实现这一点

activity.runOnUiThread(java.lang.Runnable {
    //put code here that needs to be run on the ui thread
})
在你的情况下,一个例子是

    private fun findServer(): Boolean {
   
    if(canPingServer()) {
        activity.runOnUiThread(java.lang.Runnable {
            Toast.makeText(context, "We are connected to the server server", Toast.LENGTH_LONG).show()
            gView.button_upload.setText("Upload")
            gView.button_upload.isEnabled = true
            progressDialog!!.dismiss()
        })
        return true
    } else {
        activity.runOnUiThread(java.lang.Runnable {
            Toast.makeText(context, "We cannot connect to the server.", Toast.LENGTH_LONG).show()
            gView.button_upload.setText("Server not found")
            gView.button_upload.isEnabled = false
            progressDialog!!.dismiss()
        })

        return false
    }
}