0

我遇到的问题是 PostExecute 没有触发。我看到了背景的日志标签,但 PE 从不触发。

我正在从这样的计时器调用此任务:

      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();

        }

     }

这是使用“provider.refreshNoCache();”调用的 dataprovider 类内部的主要任务本身 多于:

// 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);

        }


    }

}
4

2 回答 2

1

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

于 2012-06-26T10:43:39.997 回答
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);

    }

}
于 2012-06-27T08:43:45.933 回答