使用postExec的Android Task AsyncTask问题

时间:2012-06-26 10:12:30

标签: android android-asynctask

我遇到的问题是PostExecute没有解雇。 我看到了背景的日志标签,但是P.E.永远不会开火。

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

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

        }


    }

}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

以下是其他人可能会发现有用的解决方案:

in on create:

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

    }

}