Robospice Android - 以一个 activity 开始并以另一个 activity 结束的 spiceRequest

Robospice Android - spiceRequest that begins in one activity and ends in another activity

我正在尝试在 Activity A 中启动 robospice 请求,然后在 Activity B 中接收请求的结果。

在Activity一个

FetchSpiceRequest fetchSpiceRequest = new FetchSpiceRequest();
spiceManager.execute(fetchSpiceRequest, new ActivityB().postListener);

PostListener 实现了 PendingRequestListener 并且位于 Activity B.

中ActivityB

我们为下面 Activity A 中启动的待处理请求添加监听器。

@Override
protected void onStart() {
    spiceManager.start( this );
    spiceManager.addListenerIfPending(Post.class, 0, new PostListener());

然后我们在下面的ActivityB中实现PostListener。

public final class PostListener implements PendingRequestListener<Post> {

    @Override
    public void onRequestFailure(SpiceException spiceException) {
        Toast.makeText(MainActivity.this, "Exception: " + spiceException.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestSuccess(Post post) {
        Toast.makeText(MainActivity.this, "Added", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRequestNotFound() {
        Toast.makeText(MainActivity.this, "Request not found", Toast.LENGTH_SHORT).show();
    }
}

最后,我们在 Activity B 中创建一个变量,以便 A 可以获取它(例如,当我调用 new ActivityB().postListener 时):

public PostListener PostListener;

这不起作用,它总是在 PostListener 中调用 onRequestNotFound。

我做错了什么?

我也看过这个 post: Robospice - keep spice service continue running when changing activity 但是@Snicolas 似乎没有提到 spiceManager.execute 语句在第一个 [=51] 中应该是什么样子=].

最终,我不得不使用 LocalBroadcastManager 来实现我想要做的事情:

Activity一个

FetchSpiceRequest fetchSpiceRequest = new FetchSpiceRequest();
spiceManager.execute(fetchSpiceRequest, postListener);

请注意 postListener 位于 Activity A 中。它实际上并没有做太多,因为现在所有的工作都将在 localbroadcastreceiver 中执行:

public final class AddPostListener implements RequestListener<Post> {

    @Override
    public void onRequestFailure(SpiceException spiceException) {
        Toast.makeText(AddPostActivity.this, "Exception: " + spiceException.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestSuccess(Post post) {
        Toast.makeText(AddPostActivity.this, "Added", Toast.LENGTH_SHORT).show();
        Log.e("AddPostListener", "Background service finished"); }

在 fetchSpiceRequest 中,我们异步执行工作,一旦完成,我们调用本地广播管理器将结果发送回 Activity B:

public class FetchSpiceRequest extends SpringAndroidSpiceRequest<Post> {

    private Post mPost;
    private Context mContext;

    public AddPostSpiceRequest(Context context, Post post) {
        super(Post.class);
        mPost = post;
        mContext = context;
    }

    @Override
    public Post loadDataFromNetwork() throws Exception {
//Do some network activity to get a post and then after the network activity ends, send the post through the broadcast activity to Activity B
        Intent intent = new Intent("my-event");
        intent.putExtra("post", post);
        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);   
}

Activity B

注册您的本地广播接收器:

@Override
public void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("my-event")); }

对收到的消息做一些事情:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        Post post = intent.getParcelableExtra("post");
        Log.e("receiver", "Got message: " + post.getPostheader());
    }
};