20

我正在尝试将结果从 an 返回IntentSerivcemainactivity使用意图,但我无法让它工作。接收来自活动的IntentService意图没有问题,做它的事情并获得一个JSONstring. 现在剩下的唯一问题是将此字符串发送回活动。

这是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 不工作,但还有什么用呢?(我宁愿不使用广播接收器)。问候彼得 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
4

1 回答 1

55

为此,您可以BroadcastReceiverActivity.

这是一个很好的例子,我用来Service<>之间Activity使用BroadcastReceivers.

这是 Service <> Activity 之间通信的另一个很好的例子。它使用MessengerIncomingHandler

广播接收器

我将为您的情况做一个简单的例子。

这是您的活动的广播接收器。它将收到您的字符串:

//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()的活动中,请确保您取消注册广播接收器。

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 的意图

于 2012-10-21T11:48:00.273 回答