android应用程序在循环中显示空白屏幕(不更新ui)

android应用程序在循环中显示空白屏幕(不更新ui),android,Android,在我的应用程序中,客户端连接到服务器。它将等待,直到与服务器的连接发生。在此期间,应用程序没有响应。我怎样才能解决这个问题。尝试过的代码片段如下所示 public Connection(){ client.SetParent(this); this.context = g.getContext(); bConnected = false; mNetworkRunner = new Runnable() { public void run() {

在我的应用程序中,客户端连接到服务器。它将等待,直到与服务器的连接发生。在此期间,应用程序没有响应。我怎样才能解决这个问题。尝试过的代码片段如下所示

public Connection(){
    client.SetParent(this);
    this.context = g.getContext();
    bConnected = false;

    mNetworkRunner = new Runnable() {
        public void run() {
            try {
                Log.e("", "mNetworkRunner...");

                if( SendKeepAlive()){
                    Main.conStatus(1);
                    Log.e("", "SendKeepAlive...");
                }
                else {
                    Main.conStatus(0);
                    Log.e("", "No connection...");

                    g.log("Connection to server is lost... Trying to Connect...");
                    while(true){
                        Log.e("", "In while loop...");

                        if(!Connect()){
                            g.log("Trying...");
                            Log.e("", "In Connect no connect...");
                            Thread.sleep(2000);
                        }
                        else {
                            g.log("Connected");
                            break;
                        }

                    }
                    Main.conStatus(1);
                }
                mNetworkHandler.postDelayed(this, 30000);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    };      

}
// 
private void CheckNetworkConnection(){
    if( mNetworkHandler == null ){
        mNetworkHandler = new Handler();
        mNetworkHandler.post(mNetworkRunner);
        Log.e("", "CheckNetworkConnection...");
    }       
}

mNetworkHandler=new Handler()将使Runnable在UI线程上执行,您需要HandlerThread

private void CheckNetworkConnection(){
    if( mNetworkHandler == null ){
        HandlerThread handlerThread = new HandlerThread("thread");
        handlerThread.start();
        mNetworkHandler =  new Handler(handlerThread.getLooper());
        mNetworkHandler.post(mNetworkRunner);
        Log.e("", "CheckNetworkConnection...");
    }
}

mNetworkHandler=new Handler()将使Runnable在UI线程上执行,您需要HandlerThread

private void CheckNetworkConnection(){
    if( mNetworkHandler == null ){
        HandlerThread handlerThread = new HandlerThread("thread");
        handlerThread.start();
        mNetworkHandler =  new Handler(handlerThread.getLooper());
        mNetworkHandler.post(mNetworkRunner);
        Log.e("", "CheckNetworkConnection...");
    }
}

您在UI线程中做了大量耗时的工作,这会产生问题。在这种情况下,应该使用AsyncTask

AsyncTask允许正确且轻松地使用UI线程。此类允许在UI线程上执行后台操作和发布结果,而无需操纵线程和/或处理程序

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {

    //do your time consuming task here
     }

     protected void onProgressUpdate(Integer... progress) {
         //setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         //showDialog("Downloaded " + result + " bytes");
     }
 }

您在UI线程中做了大量耗时的工作,这会产生问题。在这种情况下,应该使用AsyncTask

AsyncTask允许正确且轻松地使用UI线程。此类允许在UI线程上执行后台操作和发布结果,而无需操纵线程和/或处理程序

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {

    //do your time consuming task here
     }

     protected void onProgressUpdate(Integer... progress) {
         //setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         //showDialog("Downloaded " + result + " bytes");
     }
 }

您正在UI线程(主线程)上执行阻塞操作。使用解决您的问题。您正在UI线程(主线程)上执行阻塞操作。通过使用解决您的问题。