将服务意图发送到活动

时间:2012-10-21 11:40:30

标签: android service android-intent intentservice

我正在尝试使用意图将结果从IntentSerivce返回到mainactivity,但我无法让它工作。 IntentService从活动中接收到没有问题的意图,这是真的,并获得JSONstring。现在唯一的问题是将此String发送回活动。

以下是mainactivity中的方法:

public String RTConn(String query){
    System.out.println("Querying Rotten Tomatoes.");
    Intent rtIntent = new Intent(MainActivity.this, RTConnection.class);
    rtIntent.putExtra("query", query);
    bindService(rtIntent, RTConnection, Context.BIND_AUTO_CREATE);
    startService(rtIntent);
    String json = getIntent().getStringExtra("json");
    return json;

这是IntentService:

public void onStart(Intent intent, int startId){
    System.out.println("intent Received");
    String jsonString = rottenTomatoesSearch(intent.getStringExtra("query"));
    Intent RTRetur = new Intent(RTConnection.this, MainActivity.class);
    RTRetur.putExtra("json", jsonString);
    startActivity(RTRetur);
}

显然startActivity不起作用,但还有什么可以使用? (我宁愿不使用广播接收器)。 关心Peter L.

更新

这就是MainActivity的外观:

public static final String RECEIVE_JSON = "student.kau.RECEIVE_JSON";
private BroadcastReceiver RTReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("broadcast Received: "+intent.getAction());
        if(intent.getAction().equals(RECEIVE_JSON)){
            System.out.println("JSON Received.");
            String serviceJsonString = intent.getStringExtra("json");
        }
    }
};

此代码是onCreate:

LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(RECEIVE_JSON);
    bManager.registerReceiver(RTReceiver, intentFilter);

这是方法:

public void onStart(Intent intent, int startId){
    System.out.println("intent Received");
    String query = intent.getStringExtra("query");
    String jsonString = rottenTomatoesSearch(query);
    System.out.println("Fetching done");
    Intent RTRetur = new Intent(MainActivity.RECEIVE_JSON);
    System.out.println("action: "+RTRetur.getAction());
    RTRetur.putExtra("json", jsonString);
    sendBroadcast(RTRetur);

}

logcat中的最后一条跟踪是System.out.println("action: "+RTRetur.getAction()");

-action: 

    student.kau.RECEIVE_JSON

1 个答案:

答案 0 :(得分:51)

为此,您可以在Activity中使用BroadcastReceiver

Here is a great example I useService<>之间持续沟通Activity使用BroadcastReceivers

服务<>之间的

Here is another great example通信活动。它使用MessengerIncomingHandler

广播接收器

我会为你的案例做一个简单的例子。

这是您的Activity的BroadcastReceiver。它会收到你的字符串:

//Your activity will respond to this action String
public static final String RECEIVE_JSON = "com.your.package.RECEIVE_JSON";

private BroadcastReceiver bReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(RECEIVE_JSON)) {
            String serviceJsonString = intent.getStringExtra("json");
            //Do something with the string
        }
    }
};
LocalBroadcastManager bManager;

在活动的onCreate()

bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_JSON);
bManager.registerReceiver(bReceiver, intentFilter);

在活动的onDestroy()中,请确保取消注册broadcastReceiver。

bManager.unregisterReceiver(bReceiver);

最后,在你的Service onStart()中,执行此操作:

System.out.println("intent Received");
String jsonString = rottenTomatoesSearch(intent.getStringExtra("query"));
Intent RTReturn = new Intent(YourActivity.RECEIVE_JSON);
RTReturn.putExtra("json", jsonString);
LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);

并且您的Activity将以其中的json作为额外的

接收意图