1

我在我的应用程序中处理一个线程,我遇到了线程等待功能的问题。我使用创建并显示我的 UI runOnUIThread(),我的主线程正在等待我的 UI 线程完成,线程完成后我在主线程中做了一些处理,这里的问题是主进程始终处于等待状态,即使我的线程完成它;我正在notify()为此使用等待和函数

      public String LSVerification() {

        String t = null;

            t="Sample string";
            synchronized(this)

            {
            AndroidHTMLActivity.this.runOnUiThread(ShowDialog) ;//here i call my thread

            try {
               this.wait();//here i waiting for my thread to finish it's job

            //here i do some process after my thread finish it's job
              //the problem is here main process always in wait state


                } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            }
          return t;        
        }

        private Runnable ShowDialog = new Runnable() {
            public void run() {
                String t = null;
        synchronized(ShowDialog)

            {
            Recognition recg=new Recognition(Activity.this);

                recg.show();


            this.notify();
            }

            }
        };
4

1 回答 1

1

在后台执行作业后尝试使用 Hanlders 更新 UI...

链接到处理程序请参阅下面的代码..

private void performOperationInBackgroundThread() {
        Thread Servicethread = new Thread(
                    new Runnable() {
                        public void run() {
                            try {
                                PerformThreadOperation();
                                DataLoaded = true;
                            } catch (Exception e) {
                                ExceptionOccured = true;
                                e.printStackTrace();
                                System.out.println(e.getMessage());
                            }
                            handler.sendMessage(handler.obtainMessage());
                        }
                    }
                    );
            Servicethread.start();
        }


 static class MyHandler extends Handler{
                MyActiviy parentActivity;
                @Override
                public void handleMessage(Message msg){
                    // Update your UI here

                    }
                }

                MyHandler(MyActiviy activity){
                    parentActivity = activity;
                }
            }

MyHandler handler = new MyHandler(this);
于 2012-11-30T10:55:58.517 回答