2012-06-26 47 views
0

问题我遇到的是PostExecute没有解雇。 我看到日志标记的背景,但P.E.从不开火。Android任务AsyncTask与postExec问题

我从一个定时器调用这个任务是这样的:

 findViewById(R.id.buttonstart).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       try { 
        openFile("FeedTimerTask.html"); 
        Timer t = new Timer("FeedTimerTask", true); 
        timerTask = new FeedTimerTask(); 
        t.schedule(timerTask, 2000, 20000); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 

Runnable runme = new Runnable() { 

     @Override 
     public void run() { 
      timestart = Calendar.getInstance().getTimeInMillis(); 
      provider.refreshNoCache(); 


     } 
    }; 

    class FeedTimerTask extends TimerTask{ 

     @Override 
     public void run() { 
      try{Looper.prepare();}catch(Exception e){}; 
      runme.run(); 

     } 

    } 

这是从调用DataProvider类内的主要任务本身“provider.refreshNoCache();”以上:

// threaded rteftesh tasks 
@SuppressWarnings("rawtypes") 
public class RefreshTask extends SupportAsyncTask { 

    private int errorcodecode = 0; 
    private ProgressDialog dialog=null; 
    private Exception mainExeption=null; 
    protected String waitMessage = "Laddar ner information.."; 
    private boolean useCache; 

    public RefreshTask(boolean useCache) { 
     this.useCache = useCache; 
    } 

    public void onPreExecute() { 
     data = null; 
     if (showSpinnerOnRefresh){ 
      dialog = ProgressDialog.show(context, "", waitMessage , true); 
      dialog.show(); 
     } 
    } 

    protected Object doInBackground(Object... params) { 
     errorcodecode = 1; 
      try { 
       invokeFeedRead(); 
       Log.e("DataProvider", "Bkgtask..."); 
       errorcodecode = 0; 
      } catch (BrJSONException e) { 
       Log.e("[ERROR]","PROVIDER "+e.getMessage()); 
       mainExeption = e; 
       errorcodecode = 1; 
      } catch (IOException e) { 
       Log.e("[ERROR]","PROVIDER "+e.getMessage()); 
       mainExeption = e; 
       errorcodecode = 2; 
      } catch (Exception e) { 
       Log.e("[ERROR]","PROVIDER "+e.getMessage()); 
       mainExeption = e; 
       errorcodecode = 3; 
      } 

     if (errorcodecode==0){ 
     } 
     return null; 
    } 

    @Override 
    protected void onCancelled() { 

     super.onCancelled(); 
     Log.e("DataProvider", "Cancelled..."); 

     if (dialog != null) 
      try{dialog.dismiss();}catch(Exception e){} 
     BrAlert.Show(context, "Obs", BrAppConfig.ServerError+" (timeout)", 0); 
     onError_IO(new IOException("Timeout!")); 
     errorcodecode=2; 


    } 

    @Override 
    protected void onPostExecute(Object result) { 
     // super.onPostExecute(result); 
     Log.e("DataProvider", "PostExec..."); 

     if (dialog != null) 
      try{dialog.dismiss();}catch(Exception e){} 

     switch (errorcodecode) { 
     case 0: 
      onFeedLoaded();    
      cacheAge = System.currentTimeMillis(); 
      break; 
     case 1: 
      onError_DataFormat(mainExeption); 
      break; 
     case 2: 
      onError_IO(mainExeption); 
      break; 
     default: 
      onError_GeneralExeption(mainExeption); 

     } 


    } 

} 
+0

调用super.onPostExecute() –

回答

1

甚至在它到达onPostExecte方法之前,您的任务被取消。如果任务在到达onPostExecute方法之前被取消。它不会触发onPostExecute,但触发onCancelled方法。请提供足够的时间来完成任务。

+0

事情是,在OnCancelled事件在不触发或者,我什么也看不到在日志中。 –

+0

谢谢,您的评论让我思考线程范围,我解决了它。上面的答案。 –

0

我最终发现了这个问题。这与范围有关。 我需要一个处理程序来调用另一个线程。

这是对他人的解决方案可能有用:

在创建:

   tickHandler = new Handler(); 
       tickTimer = new Timer(); 
       tickTimer.schedule(new FeedTimerTask(), 
           0, 
           50000); //FPS 

的处理程序类。

class FeedTimerTask extends TimerTask{ 
    private Runnable runable; 

    public FeedTimerTask(){ 
     super(); 
     runable = new Runnable(){ 

      @Override 
      public void run() { 
       timestart = Calendar.getInstance().getTimeInMillis(); 
       provider.refreshNoCache(); 

      } 

     }; 
    } 
    @Override 
    public void run() { 
     tickHandler.post(runable); 

    } 

}