我试图在活动A中启动一个robospice请求,然后在活动B中收到请求的结果。
在活动A中
FetchSpiceRequest fetchSpiceRequest = new FetchSpiceRequest();
spiceManager.execute(fetchSpiceRequest, new ActivityB().postListener);
PostListener实现了PendingRequestListener并且位于Activity B中。
在活动B中
我们为在下面的活动A中启动的待处理请求添加了Listener。
@Override
protected void onStart() {
spiceManager.start( this );
spiceManager.addListenerIfPending(Post.class, 0, new PostListener());
然后我们在下面的活动B中实现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。
我做错了什么?
我也看过这篇文章:Robospice - keep spice service continue running when changing activity但是@Snicolas似乎没有提到spiceManager.execute语句在第一个活动中应该是什么样子。
答案 0 :(得分:0)
最终,我不得不使用LocalBroadcastManager来实现我想要做的事情:
活动A
FetchSpiceRequest fetchSpiceRequest = new FetchSpiceRequest();
spiceManager.execute(fetchSpiceRequest, postListener);
请注意,postListener位于活动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中,我们异步执行工作,一旦完成,我们调用localbroadcast管理器将结果发送回活动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);
}
活动B
注册localBroadcast接收器:
@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());
}
};