不调用Asynctask onPostExecute

时间:2014-07-21 06:59:13

标签: android android-asynctask

我的程序在大多数andriod手机中运行良好。但最近它在一些特定的手机中发现了错误。我发现错误应该是由AsyncTask引起的。不调用onPostExecute。请帮忙。非常感谢!!

    public void onCreate(Bundle savedInstanceState) {
    this.setDebugTag(DEBUGTAG);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);


    try {
        final View v_splashLayout = findViewById(R.id.splashLayout);
        if(v_splashLayout== null){
            FunctionUtil.logD(DEBUGTAG, " v_splashLayout :");
        }
        v_splashLayout.post(new Runnable() {
            @Override
            public void run() {
                Rect rect = new Rect();
                Window win = getWindow();  // Get the Window
                win.getDecorView().getWindowVisibleDisplayFrame(rect);
                int statusBarHeight = rect.top;

            }
        });
    } catch(Exception e) {
        FunctionUtil.logE(DEBUGTAG,"v_splashLayout error", e);
    }


    // init circleProgressBar
    pb1 = (ProgressBar) findViewById(R.id.circleProgressBar);

    dbh = (DatabaseHelper) OpenHelperManager.getHelper(this, DatabaseHelper.class);

    if (FunctionUtil.ISDEBUGGING) {
        checkSystemConfig();
        //checkdb
    }


    init_neutralButtonOnClickListener();
    init_quitButtonOnClickListener();

    uiHandler = new MainHandler();

    callAsyncGetLatestUpdateWS();

} // END, onCreate





private void callAsyncGetLatestUpdateWS() {
    // check internet connection
    // if isOnline, check the local db version with the server version
    // if there is update, download and update the local db

    boolean isOnline = FunctionUtil.checkInternetConnection(this);
    boolean isBackgroundEnabled = FunctionUtil.checkBackgroundData(this);
    FunctionUtil.logD(DEBUGTAG, "callAsyncGetLatestUpdateWS - isOnline="+isOnline+" , isBackgroundEnabled="+isBackgroundEnabled);

    if (isOnline && isBackgroundEnabled) {

        try {
            // call webservice and update status
            // call async task to do
            if (checkVersionTask==null) {
                checkVersionTask = new CheckVersionFromWS_Task();
            }

            if (checkVersionTask.getStatus().equals(AsyncTask.Status.PENDING)) {
                // AsyncTask.Status.PENDING
                checkVersionTask.execute();
            } else if (checkVersionTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
                // AsyncTask.Status.FINISHED
                checkVersionTask.cancel(true);
                checkVersionTask = null;
                checkVersionTask = new CheckVersionFromWS_Task();
                checkVersionTask.execute();
            } else {
                // AsyncTask.Status.RUNNING
                //checkVersionTask.cancel(true);
            }
        } catch(Exception e) {
            FunctionUtil.logE(DEBUGTAG, "callAsyncGetLatestUpdateWS error:", e);
        }

        // 2. go to MainActivity, temp enable
        //goNext();

    } // END, if isOnline
    else {
        if (!isOnline) {
            // no connection
            //showDialog(Constant.DIALOG_NOCONNECTION);
            AlertDialogFactory.show(SplashActivity.this, Constant.DIALOG_NOCONNECTION, neutralButtonOnClickListener, true);
        } else if (!isBackgroundEnabled) {
            //no background
            //showDialog(Constant.DIALOG_NOBACKGROUNDDATA);
            AlertDialogFactory.show(SplashActivity.this, Constant.DIALOG_NOBACKGROUNDDATA, neutralButtonOnClickListener, true);
        }
    }
} // END, callAsyncGetLatestUpdateWS

private class CheckVersionFromWS_Task extends AsyncTask<Void, Void, LatestUpdateResult> {
    // task start
    @Override
    protected void onPreExecute() {
        FunctionUtil.logD(DEBUGTAG,getClass().getSimpleName() + " onPreExecute");

        if (pb1!=null) {
            pb1.setIndeterminate(true);
            pb1.setVisibility(View.VISIBLE);
        } else {
            pb1 = (ProgressBar) findViewById(R.id.circleProgressBar);
            pb1.setIndeterminate(true);
            pb1.setVisibility(View.VISIBLE);
        }

        isBigPromotionLoadingTimeout = false;
    } // END, onPreExecute

    @Override
    protected LatestUpdateResult doInBackground(Void... arg0) {
        FunctionUtil.logD(DEBUGTAG, getClass().getSimpleName()+" doInBackground");
        LatestUpdateResult response = null;

        if (!isCancelled()) {

            response = Util.getLatestUpdateWS(SplashActivity.this);

            if (response!=null && StatusCode.WS_SUCCESSFUL==response.getErrorCode()) {

                try {
                    //load the DB data of Promotion
                    long st = System.currentTimeMillis();
                    boolean b = Util.callGetPromotionLinkWS(SplashActivity.this, dbh);
                    long et = System.currentTimeMillis();
                    FunctionUtil.logD(DEBUGTAG, getClass().getSimpleName()+" doInBackground : Util.callGetPromotionLinkWS : "+b+" , load time="+(et-st)+"ms");

                    if (b) {
                        preLoadBigBanner();


                        int sleepTime = 500;
                        int totalSleepTime = 0;
                        while(!isFinishLoadingBigPromotion && totalSleepTime < Constant.BIGPROMOTIONWAITTIME){
                            FunctionUtil.logD(DEBUGTAG, "SplashActivity wait isFinishLoadingBigPromotion: "+isFinishLoadingBigPromotion);
                            try {
                                FunctionUtil.logD(DEBUGTAG, "SplashActivity wait for bigpromotion loading time : "+totalSleepTime);
                                totalSleepTime = totalSleepTime+sleepTime;

                                SystemClock.sleep(sleepTime);

                            } catch(Exception ex){
                                totalSleepTime = Constant.BIGPROMOTIONWAITTIME;
                                FunctionUtil.logE(DEBUGTAG, "doInBackground loadingBigPromotion error:", ex);
                                break;
                            }
                        } // END, while
                        isBigPromotionLoadingTimeout = true;
                    } // END, if

                } catch(Exception e) {
                    FunctionUtil.logE(DEBUGTAG, getClass().getSimpleName()+" doInBackground error:", e);
                }
            } // END, if

        } // END, isCancelled
        return response;
    } // END, doInBackground

    @Override
    protected void onPostExecute(LatestUpdateResult result) {
        FunctionUtil.logD(DEBUGTAG, getClass().getSimpleName() + " onPostExecute > result="+result);

        if(pb1 != null) {
            pb1.setVisibility(View.GONE);
        }

        if (result!=null) {

            int statusCode = result.getErrorCode();

            if (statusCode==StatusCode.WS_SUCCESSFUL) {

                Intent i1 = null;
            /***

            **/

            goNext();
        }


    }

0 个答案:

没有答案