【问题标题】:how to call intent in asynctask? or how to start new activity in onPostExecute?如何在异步任务中调用意图?或者如何在 onPostExecute 中开始新的活动?
【发布时间】:2016-06-11 09:15:54
【问题描述】:

有人可以帮助我如何在 onPostExecute 中调用新活动或在 asynctask 中添加意图吗?

我的代码是这样的..

backgroundtask.java

AlertDialog alertDialog;

Context ctx;

BackgroundTask(Context ctx) {
    this.ctx = ctx;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle("Login Information..");
}

@Override
protected String doInBackground(String... params) {
    String reg_url = "http://10.0.2.2/webapp/register.php";
    String login_url = "http://10.0.2.2/webapp/login.php";
    String method = params[0];
    if (method.equals("register")) {

        String name = params[1];
        String user_name = params[2];
        String user_pass = params[3];

        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
            String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                    URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                    URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();

            IS.close();
            return "Registration Success..";

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } else if (method.equals("login")) {//EDITED

        String login_name = params[1];
        String login_pass = params[2];

        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String data = URLEncoder.encode("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" +
                    URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String response = "";
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                response += line;
            }


            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return response;


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    return null;
}


@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
    if (result.equals("Registration Success..")) {
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
        //startActivity(new Intent(this, welcome.class));



    } else {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

}

}

MainActivity.java 这是我的登录类

EditText ET_NAME, ET_PASS;
String login_name, login_pass;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    ET_NAME = (EditText)findViewById(R.id.user_name);
    ET_PASS = (EditText)findViewById(R.id.user_pass);

}
public void userReg(View view) {
    startActivity(new Intent(this,Register.class));

}
public void userLogin(View view) {

    login_name = ET_NAME.getText().toString();
    login_pass = ET_PASS.getText().toString();
    String method = "login";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method, login_name, login_pass);
}

我是android新手,希望你能帮助我..这段代码没有错误顺便说一句..我只想调用另一个类而不是一个显示欢迎的对话框..

【问题讨论】:

  • 你遇到了什么问题?
  • ctx.startActivity(new Intent(ctx, welcome.class));onPostExecuteif 条件中添加这一行

标签: android android-intent android-asynctask


【解决方案1】:

你可以用这个

@Override
protected void onPostExecute(String result) {
  if (result.equals("Registration Success..")) {
    Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();

    Intent login = new Intent(ctx, MainActivity.class);
    login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    ctx.startActivity(login);
    ((Activity) ctx).finish();


  } else {
    alertDialog.setMessage(result);
    alertDialog.show();
  }

}

【讨论】:

    【解决方案2】:

    在您的 postExecute 方法中。

    @Override
    protected void onPostExecute(String result) {
        if (result.equals("Registration Success..")) {
    
            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(ctx, welcome.class);
            startActivity(intent);
    
        } else {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    }
    

    【讨论】:

    • 嗨..感谢您提供的出色代码先生..似乎问题出在我的“字符串响应”中..我应该在我的'if(result.equals(“欢迎.."))' "Welcome.." 在我的 PHP 文件中..
    【解决方案3】:

    AsyncTaskonPostExecute 方法中执行此操作

    if (result.equals("Registration Success..")) {
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
        ctx.startActivity(new Intent(ctx, welcome.class));
    }
    

    它会打开新的活动。

    【讨论】:

      【解决方案4】:

      仅使用您的 Contextctx 在您的情况下的意图,

      Intent intent = new Intent(ctx, Welcome.class);
              ctx.startActivity(intent);
              ((Activity)ctx).finish();
      

      【讨论】:

        【解决方案5】:

        使用ctx.startActivity(intent) 开始活动。

        【讨论】:

          猜你喜欢
          • 2017-01-05
          • 2012-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-20
          • 1970-01-01
          相关资源
          最近更新 更多