2014-10-17 49 views
1

問題: AsyncTask狀態未指定爲FINISHED,但在執行doInBackground()中的代碼後仍保持RUNNING狀態(應用程序在啓動此任務後運行緩慢,它非常耗時)AsyncTask狀態未指定爲完成

我需要完成。我做錯了什麼?

感謝

代碼(shortend):

MyTask mt; 

    class MyTask extends AsyncTask<Void, Void, Void> { 

     int myProgressCount; 



     @Override 
     protected void onPreExecute() { 
       super.onPreExecute(); 

       Toast.makeText(DeviceUARTContext, "onPreExecute Start Progress Bar", Toast.LENGTH_LONG).show(); 

     }  

     @Override 
     protected Void doInBackground(Void... params) { 

       // the code of a task 

      return null; 
     } 



     @Override 
     protected void onPostExecute(Void result) { 
       super.onPostExecute(result); 

       Toast.makeText(DeviceUARTContext, "onPostExecute End Progress Bar", 
          Toast.LENGTH_LONG).show(); 



       Toast.makeText(DeviceUARTContext, "asynctask status: "+mt.getStatus(), Toast.LENGTH_LONG).show(); 
       // here mt.getStatus() should be "Finished" 


     } 
} 





ReadDataBut = (Button) view.findViewById(R.id.button12); 
    ReadDataBut.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      mt = new MyTask(); 
      mt.execute(); 

     } 
    }); 
+0

你在問爲什麼你的代碼一直在運行,但你沒有顯示代碼... – 2Dee 2014-10-17 11:35:53

回答

-1
protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 

      Toast.makeText(DeviceUARTContext, "onPostExecute End Progress Bar", 
         Toast.LENGTH_LONG).show(); 



      Toast.makeText(DeviceUARTContext, "asynctask status: "+mt.getStatus(), Toast.LENGTH_LONG).show(); 
      // здесь mt.getStatus() должно быть "Finished" 


    } 

如果你的麪包 「的AsyncTask狀態:」 正在diplayed這意味着你已經在onPostExecute(),即你的doInBackground() methd已經完成,並且在這個烤麪包被顯示之後,即onPostExecute()方法完成執行時它會結束。

請參閱此鏈接:http://developer.android.com/reference/android/os/AsyncTask.Status.html

它指出了onPostExecuted後明確()已經完成它的執行狀態的改變,你都出現在同一個方法敬酒完成之前,因此跑步返回onPostExecuted()尚未完成。

+0

但mt.getStatus()是「正在運行」 - 這意味着任務沒有執行。 – 2014-10-17 11:25:47

+0

不!任務保持在「正在運行」 – 2015-12-16 19:28:10