2014-10-17 33 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