从服务发送意图活动(send intent from service to activity)

2019-06-18 14:06发布

我试图从返回的结果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

Answer 1:

对于这一点,您可以使用BroadcastReceiver在你的Activity

这里是一个很好的例子我使用之间连续地进行通信Service <> Activity使用BroadcastReceivers

下面是另一个很好的例子服务<>活动之间的通信。 它使用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);

和你的活动将获得与JSON的意图,它作为一个额外的



文章来源: send intent from service to activity