根据异步任务的结果,转移到多个新活动的正确方法

时间:2013-02-25 23:34:25

标签: android android-asynctask

我在AsyncTask中与服务器进行了一些通信。现在,根据我想要的数据,我要么转到屏幕A或屏幕B.这样做的正确方法是什么?

我应该在postExcute部分调用新活动吗?我现在正处于启动画面。

private class CheckLoginDetails extends AsyncTask<Void, String, String> {


        protected void onPreExecute() {
                //does stuff before excuting code
        }

        //does the main operation
        protected String doInBackground(Void... params)  {

            publishProgress("10", "Checking Login Details Exist . . .");

            if(checkForLogin()){    
                //validate with DB
                publishProgress("20", "Logging in, verifying . . .");
                if(sendforVerif()){
                    publishProgress("100", "You Are Logged In!");

                }else{                  
                    publishProgress("100", "Error Logging In.");

                }

            }else{
                //login or registration required
                publishProgress("100", "Login Required . . .");
            }

            return null;
        }

        //Update Progress       
        protected void onProgressUpdate(String... values) {
            mProgress.setProgress(Integer.parseInt(values[0]));
            statusReadout.setText(values[1]);
        }

        //Update UI with results
        protected void onPostExecute() {
            Intent intent = new Intent(SplashScreen.this, MenuScreen.class);
            SplashScreen.this.startActivity(intent);
        }

    }

我不认为这是对的,加上它不起作用!?

TIA

1 个答案:

答案 0 :(得分:2)

目前,您并未覆盖onPostExecute AsyncTask方法{1}}将您的代码更改为:

private class CheckLoginDetails extends AsyncTask<Void, String, String> {

@Override
    protected void onPreExecute() {
            //does stuff before excuting code
    }

    //does the main operation
@Override
    protected String doInBackground(Void... params)  {

             // your code here...
        return null;
    }

    //Update Progress 
@Override     
    protected void onProgressUpdate(String... values) {
  // your code here...
    }
   @Override
    protected void onPostExecute(String result ) {
        Intent intent = new Intent(SplashScreen.this, MenuScreen.class);
        SplashScreen.this.startActivity(intent);
    }

}