如何从一个IntentService回一个活动的结果吗?

问题描述:

我使用的是IntentService来处理网络通信通过JSON的服务器。该JSON /服务器部分工作正常,但我遇到了麻烦,结果后面需要的地方。下面code说明我是如何从内部的onClick()启动的目的服务,并具有该服务更新一个全局变量中继结果反馈给主要活动。

I am using an IntentService to handle network communications with a server via JSON. The JSON/server part is working fine, but I'm having trouble getting the results back where they are needed. The following code shows how I am starting the intent service from inside onClick(), and then having the service update a global variable to relay the results back to the main activity.

public class GXActivity extends Activity {

    private static final String TAG = "GXActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // === called when the activity is first created

        Log.i(TAG, "GXActivity Created");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);

        View.OnClickListener handler = new View.OnClickListener() {
            public void onClick(View v) {

                // === set up an application context to hold a global variable
                // === called user, to contain results from the intent service
                GXApp appState = ((GXApp) getApplicationContext());
                User user = new User(-1); // initialize user id to -1
                appState.setUser(user);

                // === next start an intent service to get JSON from the server;
                // === this service updates the user global variable with
                // === results from the JSON transaction
                Intent intent = new Intent(this, GXIntentService.class);
                startService(intent);

                // === check app context for results from intent service
                appState = ((GXApp) getApplicationContext());
                if (appState.getUser().getId() != -1)...

            }
        }
    }
}

我遇到的问题是,意图服务,解析JSON不会被调用,直到后的onCreate()完成,所以我的code,它要寻找的结果是停留在看未初始化的结果。

The problem I'm having is that the intent service that parses the JSON doesn't get invoked until after onCreate() completes, so my code that's looking for the results is stuck looking at uninitialized results.

我应该怎么做不同这样的意图服务被调用之前我检查结果如何?将它的工作,如果我把点击听者出的onCreate()函数?是否有另一个/更好地构建这种code?非常感谢。

What should I do differently so the intent service gets called before I check the results? Would it work if I pulled the click listener out of the onCreate() function? Is there a another/better to structure this code? Many thanks.

您应该考虑创建自己的ResultReceiver$c$c>子类中的活动。 ResultReceiver 工具 Parcelable ,以便可以从你的活动传递来你的服务作为一个额外的意图

You should look at creating your own ResultReceiver subclass in your activity. ResultReceiver implements Parcelable so can be passed from your Activity to your Service as an extra on the Intent.

您需要做的是这样的:

  1. 实现您的活动类中的 ResultReceiver 的子类。实施的主要手段是 onReceiveResult()。此方法为您提供了一个与捆绑可以用来传递任何信息要检索的结果数据的服务 。简单地解压缩你所需要的数据,并用它来更新你的活动。

  1. Implement a subclass of ResultReceiver within your activity class. The key method to implement is onReceiveResult(). This method provides you a with Bundle of result data which can be used to pass whatever information you are retrieving in your Service. Simply unpack the data you need and use it to update your activity.

在你的活动,创建自定义的 ResultReceiver 一个新实例,并将其添加到意图你用它来启动服务。

In your activity, create a new instance of your custom ResultReceiver and add it to the Intent you use to start your service.

在你的服务 onStartCommand()方法,检索 ResultReceiver 通过了关于意图,并将其存储在本地成员变量。

In your Service's onStartCommand() method, retrieve the ResultReceiver passed in on the Intent and store it in a local member variable.

在你的服务完成其工作,有它叫发送()在ResultReceiver传递什么数据要回送活动在捆绑

Once your Service completes its work, have it call send() on the ResultReceiver passing whatever data you want to send back to the activity in a Bundle.

这是一个pretty的有效模式,意味着你不存储在讨厌的静态变量的数据。

This is a pretty effective pattern and means you're not storing data in nasty static variables.