我有这个 IntentService (HttpService
),它从 Web 服务获取原始 json 数据:
public class HttpService extends IntentService {
public static final String BROADCAST_ACTION = "com.example.HttpService";
public HttpService() {
super("HttpService");
}
@Override
protected void onHandleIntent(Intent intent) {
//code to get a String with jsonData
//Then I send a broadcast
Intent broadcastHttpResponseIntent = new Intent();
broadcastHttpResponseIntent.setAction(BROADCAST_ACTION);
broadcastHttpResponseIntent.putExtra("jsonData", jsonData);
sendBroadcast(broadcastHttpResponseIntent);
}
}
现在,从使用 HttpService
的 IntentService 中,我尝试获取广播:
public class RestaurantModel extends IntentService {
public static final String BROADCAST_ACTION = "com.example.RestaurantModel";
public RestaurantModel() {
super("RestaurantModel");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v("RestaurantModel", "onHandleIntent");
BroadcastReceiver httpBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("RestaurantModel", "onReceive");
String jsonResponse = intent.getStringExtra("jsonData");
}
};
Intent getRestaurantsJsonIntent = new Intent(RestaurantModel.this, HttpService.class);
getRestaurantsJsonIntent.putExtra("urlRestaurants", intent.getStringExtra("urlRestaurants"));
startService(getRestaurantsJsonIntent);
registerReceiver(httpBroadcastReceiver, new IntentFilter(HttpService.BROADCAST_ACTION));
}
}
所以我收到此错误:
RestaurantModel has leaked IntentReceiver com.example.RestaurantModel$1@42374590 that was originally registered here. Are you missing a call to unregisterReceiver()?
所以我尝试注销接收器,但似乎需要一个上下文来注销接收器。
如何从一个 IntentService 接收值到另一个 IntentService 中?
最佳答案
最好的答案是:只有一个IntentService
。
下一个最佳答案是:完全摆脱广播内容,并让第一个 IntentService
调用 startService()
来启动第二个 IntentService
.
关于android - 如何在另一个 IntentService 中从一个 IntentService 接收值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767442/