如何将AsyncTask的结果从Activity1发送到Activity2

时间:2013-12-22 10:00:59

标签: java android android-asynctask

在我的Activity1中,我有一个上传到服务器的AsyncTask。一旦启动此任务,我想启动Activity,而不必等待AsyncTask的完成。当Activity1的AsyncTask完成后,我想更新Activity2中的内容。在做了一些搜索之后,我发现了多个使用接口的参考/示例。但是我遇到了以下问题:

OnUploadCompleted接口

public interface OnUploadCompleted {
    void on UploadCompleted();
}

活性2

public class Activity2 extends Activity implements OnUploadCompleted {
    // all the usual activity code

    @Override
    public void onUploadCompleted() {
        Toast.makeText(this, "Upload Done", ....
    }
}

活性1

public class Activity1 extends Activity {
    // all the usual activity code

    private class Upload extends AsyncTask<...> {
        OnUploadCompleted listener;

        public Upload(OnUploadCompleted listener) {
            this.listener = listener;
        }

        // skipping doInBackground task

        @Override
        protected void onPostExecute(...) {
            super.onPostExecute();
            listener.onUploadCompleted();
        }
    }

    void foo (...) {
        OnUploadCompleted listener = new Activity2();
        Upload upload = new Upload(listener);
        upload.execute();
        finish();
    }
}

我遇到的问题是foo()函数。侦听器是Activity2类的新实例,但尚未创建Activity2。它将在finish()之后由Activity1的父活动创建。因此,当实际调用侦听器时,它“连接”的活动为空。在onUploadCompleted()中,当调用Toast时,“this”为空。

1 个答案:

答案 0 :(得分:1)

当Activity1的AsyncTask完成时,尝试从Activity1向ACtivity2发送广播...

public class MainActivity extends Activity {
public static final String ACTION_TASK_COMPLETED = "com.sample.project.action.ACTION_TASK_COMPLETED";

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class DoTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        // please wait. I am doing work
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // yay... work completed...
        Intent intent = new Intent(ACTION_TASK_COMPLETED);
        LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(intent);
    }
  }
}

public class SecondActivity extends Activity {
private TaskReceiver taskReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    IntentFilter intentFilter = new IntentFilter(MainActivity.ACTION_TASK_COMPLETED);
    taskReceiver = new TaskReceiver();
    LocalBroadcastManager.getInstance(this).registerReceiver(taskReceiver, intentFilter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(taskReceiver);
}

private void onUploadImage() {
    // uploading completed... 
}

private class TaskReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        onUploadImage();
    }
}
}