Java线程,冻结我的UI

Java线程,冻结我的UI,java,android,Java,Android,我试图每分钟检查2个字符串的值,然后关闭线程 当值不同时,但这段代码冻结了我的UI。这就是密码 @Override protected void onPostExecute(String result) { //Download sourcecode on String String secondaChar = result; //Strings declaration String First = ""; String Second = "";

我试图每分钟检查2个字符串的值,然后关闭线程 当值不同时,但这段代码冻结了我的UI。这就是密码

@Override
protected void onPostExecute(String result) 
{
    //Download sourcecode on String
    String secondaChar = result;
    //Strings declaration
    String First = "";
    String Second = "";
    try 
    {   
        //Writing sourcecode on string
        Write(secondaChar);
        String ReadingFile="FileSorgente";
        First=reading(ReadingFile);
        while (true) 
        {   
            // Downloading new sourcecode on string
            secondaChar = result.replaceAll("[^a-zA-Z]+", "");
            //writing new SC on string
            Scrittura(secondaChar);
            // new SC on second string
            Second = Reading(ReadingFile);
            // check until 2 strings are the same
            if(First.equalsIgnoreCase(Second))
            {
                Toast.makeText(getApplicationContext(), "Keep Checking", Toast.LENGTH_LONG).show();
            }
            else
            {         
                Toast.makeText(getApplicationContext(), "FALSE", Toast.LENGTH_LONG).show(); 
                break;
            } 
            Thread.sleep(60 * 1000);
        }
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    textView.setText("Usless");
}

读和写是两种不同的方法,我以前在java中测试过这一点,它很有效,但可能在Android sdk中我必须做一些不同的事情。

您应该在运行UI线程的
doInBackground()
中执行长时间运行的任务,例如,
Thread.sleep(…)

onPostExecute()
应该仅在
doInBackground()
完成后调用,并准确地用于更新UI-因此它在UI线程上运行。你在那里的每一次延迟都会导致你的应用程序失去响应

要解决此问题,请将逻辑移动到
doInBackground()
。但是请记住,所有的UI操作都必须在UI线程上进行。因此,例如,必须从
onPostExecute()
onProgressUpdate()
显示祝酒词


有关如何执行此操作的示例,请参见。请注意,
Thread.sleep()
也在那里被调用,但是在
doInBackground()

onPostExecute()
中,它在主线程上运行。我很确定您希望在
doInBackground()
函数中实现这一点。然后,添加了onProgressUpdate和publishProgress。更新了虚拟代码。
@Override
                protected void doInBackground(params....) 
                {
                    //Download sourcecode on String
                    String secondaChar = result;
                    //Strings declaration
                    String First = "";
                    String Second = "";
                    try 
                    {   
                        //Writing sourcecode on string
                        Write(secondaChar);
                        String ReadingFile="FileSorgente";
                        First=reading(ReadingFile);
                        while (true) 
                        {   
                            // Downloading new sourcecode on string
                            secondaChar = result.replaceAll("[^a-zA-Z]+", "");
                            //writing new SC on string
                            Scrittura(secondaChar);
                            // new SC on second string
                            Second = Reading(ReadingFile);
                            // check until 2 strings are the same
                            if(First.equalsIgnoreCase(Second))
                            {
                                  publishProgress( "Keep Checking" );
                            }
                            else
                            {         
                                  publishProgress( "FALSE" ); 
                                break;
                            } 
                            Thread.sleep(60 * 1000);
                        }
                    } 
                    catch (IOException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                     publishProgress("Usless");
                }

@Override
protected void onProgressUpdate(String... progress) {
         showToast(progress[0]);
}