从服务发送意图活动 [英] send intent from service to activity
问题描述
我想从 IntentSerivce
到 mainactivity
使用意图,但我可以返回结果弄不明白的工作。
该 IntentService
接收到来自活动的意图没有问题,做它的事,得到了 JSONstring
。现在唯一剩下的问题是这个字符串发回的活动。
下面是在mainactivity方法:
公共字符串RTConn(查询字符串){
的System.out.println(查询烂番茄。);
意图rtIntent =新的意图(MainActivity.this,RTConnection.class);
rtIntent.putExtra(查询,查询);
bindService(rtIntent,RTConnection,Context.BIND_AUTO_CREATE);
startService(rtIntent);
JSON字符串= getIntent()getStringExtra(JSON)。
返回JSON;
这里是IntentService:
公共无效ONSTART(意向意图,诠释startId){
的System.out.println(意图收到);
字符串jsonString = rottenTomatoesSearch(intent.getStringExtra(查询));
意图RTRetur =新的意图(RTConnection.this,MainActivity.class);
RTRetur.putExtra(JSON,jsonString);
startActivity(RTRetur);
}
显然startActivity不工作,但还有什么用? (我宁可不使用广播接收器)。 此致彼得·L;
更新
这是怎么了MainActivity看起来大气压:
公共静态最后弦乐RECEIVE_JSON =student.kau.RECEIVE_JSON;
私人的BroadcastReceiver RTReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
的System.out.println(广播收稿日期:+ intent.getAction());
如果(intent.getAction()。等于(RECEIVE_JSON)){
的System.out.println(JSON收到。);
串serviceJsonString = intent.getStringExtra(JSON);
}
}
};
这code是的onCreate:
LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(本);
IntentFilter的IntentFilter的=新的IntentFilter();
intentFilter.addAction(RECEIVE_JSON);
bManager.registerReceiver(RTReceiver,IntentFilter的);
这是方法:
公共无效ONSTART(意向意图,诠释startId){
的System.out.println(意图收到);
查询字符串= intent.getStringExtra(查询);
串jsonString = rottenTomatoesSearch(查询);
的System.out.println(抓取,做);
意图RTRetur =新的意图(MainActivity.RECEIVE_JSON);
的System.out.println(行动:+ RTRetur.getAction());
RTRetur.putExtra(JSON,jsonString);
sendBroadcast(RTRetur);
}
在logcat中的最后一丝的的System.out.println(行动:+ RTRetur.getAction());
清议:
student.kau.RECEIVE_JSON
对于这一点,你可以使用<一个href="http://developer.android.com/reference/android/content/BroadcastReceiver.html"><$c$c>BroadcastReceiver$c$c>在活动
。
这里是一个很好的例子我用以服务之间连续通信
&LT;> 活动
使用 BroadcastReceivers
<一个href="http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging">Here为服务℃之间沟通的另一个很好的例子;>活动。它使用使者
和 IncomingHandler
。
的BroadcastReceiver
我会做一个简单的例子对你的情况。
这是你的BroadcastReceiver为您的活动。这将是收到您的字符串:
//你的活动将这一动作字符串回应
公共静态最后弦乐RECEIVE_JSON =com.your.package.RECEIVE_JSON;
私人的BroadcastReceiver bReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(intent.getAction()。等于(RECEIVE_JSON)){
串serviceJsonString = intent.getExtra(JSON);
//做些什么字符串
}
}
}
在你的的onCreate()
活动的
LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(本);
IntentFilter的IntentFilter的=新的IntentFilter();
intentFilter.addAction(RECEIVE_JSON);
bManager.registerReceiver(bReceiver,IntentFilter的);
最后,在你的服务ONSTART()
,做到这一点:
的System.out.println(意图收到);
字符串jsonString = rottenTomatoesSearch(intent.getStringExtra(查询));
意图RTRetur =新的意图(YourActivity.RECEIVE_JSON);
RTRetur.putExtra(JSON,jsonString);
LocalBroadcastManager.getInstance(本).sendBroadcast(RTReturn);
和你的活动会收到与JSON的意图在里面作为一个额外的
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.
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;
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);
}
Obviously the startActivity isn't working, but what else is there to use? (I would rather not use a broadcast receiver). Regards Peter L.
Update
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");
}
}
};
This code is the onCreate:
LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_JSON);
bManager.registerReceiver(RTReceiver, intentFilter);
This is the method:
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);
}
The last trace in logcat is the System.out.println("action: "+RTRetur.getAction()");
-action:
student.kau.RECEIVE_JSON
For this, you can use a BroadcastReceiver
in your Activity
.
Here is a great example I use to communicate continuously between Service
<> Activity
using BroadcastReceivers
.
Here is another great example of communication between Service <> Activity. It uses Messenger
and IncomingHandler
.
BroadcastReceiver
I will make a quick example for your case.
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.getExtra("json");
//Do something with the string
}
}
}
In your onCreate()
of the activity
LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_JSON);
bManager.registerReceiver(bReceiver, intentFilter);
And finally, in your Service onStart()
, do this:
System.out.println("intent Received");
String jsonString = rottenTomatoesSearch(intent.getStringExtra("query"));
Intent RTRetur = new Intent(YourActivity.RECEIVE_JSON);
RTRetur.putExtra("json", jsonString);
LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);
and your Activity will receive the intent with that json in it as an extra
这篇关于从服务发送意图活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!