将意图从服务发送到活动 [英] send intent from service to activity
问题描述
我正在尝试使用 Intent 将结果从 IntentSerivce
返回到 mainactivity
,但我无法让它工作.IntentService
毫无问题地从 Activity 接收 Intent,执行操作并获取 JSONstring
.现在剩下的唯一问题是将此字符串发送回活动.
I'm trying to return the result from an IntentSerivce
to the mainactivity
using an intent, but I can't get it to work.
The IntentService
receives the intent from the activity without a problem, does it's thing and gets a JSONstring
. Now the only problem left is to send this String back to the activity.
这里是mainactivity中的方法:
Here is the method in the 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:
And here is the 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.
Obviously the startActivity isn't working, but what else is there to use? (I would rather not use a broadcast receiver). Regards Peter L.
更新
这是 MainActivity 的样子:
this it how the MainActivity looks atm:
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:
This code is the 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()");
The last trace in logcat is the System.out.println("action: "+RTRetur.getAction()");
-action:
student.kau.RECEIVE_JSON
推荐答案
为此,您可以使用 BroadcastReceiver
在您的 Activity
中.
For this, you can use a BroadcastReceiver
in your Activity
.
这是我用来在 之间持续通信的一个很好的例子
Service <> Activity
使用 BroadcastReceivers
.
Here is a great example I use to communicate continuously between Service
<> Activity
using BroadcastReceivers
.
这是服务之间通信的另一个很好的例子<> 活动.它使用 Messenger
和 IncomingHandler
.
Here is another great example of communication between Service <> Activity. It uses Messenger
and IncomingHandler
.
我会为你的案例做一个简单的例子.
I will make a quick example for your case.
这是您的 Activity 的 BroadcastReceiver.它将接收您的字符串:
This is your BroadcastReceiver for your Activity. It is going to receive your String:
//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()
中,确保取消注册广播接收器.
In your onDestroy()
of the activity, make sure you unregister the broadcastReceiver.
bManager.unregisterReceiver(bReceiver);
最后,在您的 Service onStart()
中,执行以下操作:
And finally, in your Service onStart()
, do this:
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 的 Intent 作为额外内容
and your Activity will receive the intent with that json in it as an extra
这篇关于将意图从服务发送到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!