gpt4 book ai didi

android - 如何从另一个 IntentService 中的 IntentService 接收值?

转载 作者:行者123 更新时间:2023-11-29 00:05:59 27 4
gpt4 key购买 nike

我有这个 IntentService (HttpService),它从网络服务中获取原始 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com