如何在Asynctask中回调我的父活动

时间:2015-07-21 23:46:24

标签: android android-asynctask

我使用asynctask从数据库获取用户信息,因为你不能在它的前端任务中执行...当任务完成时我想回到我称之为Asynctask的地方有任何方法可以做这个 ?

我的目标是在asynctask完成时启动另一个活动

package com.example.eightmiles;
 import android.app.AlertDialog;  
 import android.content.Context;  
 import android.os.AsyncTask;
import android.print.PrintAttributes;
import android.widget.Toast;  
 import java.io.BufferedReader;  
 import java.io.BufferedWriter;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.InputStreamReader;  
 import java.io.OutputStream;  
 import java.io.OutputStreamWriter;  
 import java.net.HttpURLConnection;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.net.URLEncoder;
import java.util.StringTokenizer;  

 public class Backgroundtask extends AsyncTask<String, Void, String> {  
  AlertDialog alertDialog;  
  AlertDialog alertDialog2;  
   Context ctx;  
   Signin signin;
   Userlocalstore userlocalstore;
   String res_name , res_username , res_dob , res_email , res_id , res_pass;
   Backgroundtask(Context ctx)  
   {  
    this.ctx =ctx;  
   }  

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

     alertDialog2.setTitle("Apologies");  
   }  

   @Override  
   protected String doInBackground(String... params) {  
     String reg_url = "http://8miles.freeiz.com/Register.php";  
     String login_url = "http://8miles.freeiz.com/getuserinfo.php";  
     String method = params[0];  
     if (method.equals("Register")) {  
       String name = params[1];  
       String user_name = params[2];  
       String user_pass = params[3];  
       String email = params[4];  
       String dob = params[5];  

3 个答案:

答案 0 :(得分:1)

OnPostExecuteAsyncTask方法中,通过Intent启动新活动,并将加载的数据传递给它。

@Override
protected void onPostExecute(String data) {
     Intent intent = new Intent(MainActivity.this, WhateverActivityToStart.class);
     intent.putExtra("SOME_DATA", data);
     MainActivity.this.startActivity(intent);
 }

您需要熟悉starting an activitypassing data between activities

答案 1 :(得分:0)

这就是onPostExecute()的用途。您可以check the docs查看第四步。

基本上,您将无法返回到调用AsyncTask的位置,因为这是异步过程的设计。

相反,因为当AsyncTask完成时,在UI线程中调用onPostExecute(),你可以使用它来启动一个活动,显示一个toast和其他东西。

答案 2 :(得分:0)

我更喜欢在尝试在AsyncTask中执行某些操作时使用回调,然后返回在我的Activity中工作...虽然这里已经接受了一个答案,但是这里有很多代码:

public class Backgroundtask extends AsyncTask<String, Void, String> { 
    private final TaskCallback taskCallback;
    private final Context ctx;
    ...
    ...

    public interface TaskCallback{
        public void taskComplete(String data);
    }

    Backgroundtask(Context ctx, TaskCallback taskCallback){  
        this.ctx = ctx;
        this.taskCallback = callback
    }

     ...
     ...

    //truncating the rest of your code, adding just the extra bit needed

    @Override
    protected void onPostExecute(String data) {
        super.onPostExecute(data);
        taskCallback(data);
    }
}

然后在你的活动中你应该有:

 public class MyActivity extends Activity implements TaskCallback{

     //again, truncating most of what would be here like onCreate, etc...

    @Override
    public void taskComplete(String data){
        //call more work here if needed

        Intent intent = new Intent(MyActivity.this, WhateverActivityToStart.class);
        intent.putExtra("SOME_DATA", data);
        startActivity(intent);

        finish(); //May or may not need this, don't want them coming back probably
    }

 }