我会直截了当地说。
我需要使用一些Google网络服务(DistanceMatrix and Geocoding APIs
),然后才能继续下一步获取两个回复。我刚刚遇到IntentServices
,我读到这可能是执行一项任务然后另一项任务的好方法,因为他们可以使用Intents
和BroadcastReceivers
进行通信。这样,第二个IntentService将知道第一个IntentService何时完成它的工作。
以下步骤总结了我想要实现的目标。
DistanceMatrix API
Geocoding API
对此进行地理编码。然而,在我做这个实现之前,测试代码向控制台发送一些消息只是为了确保两个意图服务可以相互通信,但我得到了一个无限循环。
I/MapFragment: DistanceMatrixService finished...
I/MapFragment: Running GeocodingService...
I/MapFragment: GeocodingService finished...
I/MapFragment: The whole process has finished...
I/MapFragment: DistanceMatrixService finished...
I/MapFragment: Running GeocodingService...
I/MapFragment: GeocodingService finished...
I/MapFragment: The whole process has finished...
I/MapFragment: DistanceMatrixService finished...
I/MapFragment: Running GeocodingService...
I/MapFragment: GeocodingService finished...
I/MapFragment: The whole process has finished...
I/MapFragment: DistanceMatrixService finished...
I/MapFragment: Running GeocodingService...
I/MapFragment: GeocodingService finished...
I/MapFragment: The whole process has finished...
单击按钮时会调用第一个IntentService
@Override
public void onClick(View v) {
Intent msgIntent = new Intent(getActivity(), DistanceMatrixService.class);
getActivity().startService(msgIntent);
}
DistanceMatrixService类只是将消息发送回主片段。
public class DistanceMatrixService extends IntentService {
public static final String ACTION_FINISHED = "ACTION_FINISHED";
public DistanceMatrixService() {
super("DistanceMatrixService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Run the heavy task just here
//...
// Once the job is done send a broadcast message to communicate that it has finished
Intent bcIntent = new Intent();
bcIntent.setAction(ACTION_FINISHED);
sendBroadcast(bcIntent);
}
}
当第一个IntentService完成GeocodingServices启动时
private class DistanceMatrixBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(DistanceMatrixService.ACTION_FINISHED)){
Log.i(LOG_TAG, "DistanceMatrixService finished...");
Log.i(LOG_TAG, "Running GeocodingService...");
Intent msgIntent = new Intent(getActivity(), GeocodingService.class);
getActivity().startService(msgIntent);
}
}
}
与第一个IntentService一样,这个只是向主片段发送消息
public class GeocodingService extends IntentService{
public static final String ACTION_FINISHED = "ACTION_FINISHED";
public GeocodingService() {
super("GeocodingService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Run the heavy task just here
//...
// Once the job is done send a broadcast message to communicate that it has finished
Intent bcIntent = new Intent();
bcIntent.setAction(ACTION_FINISHED);
sendBroadcast(bcIntent);
}
}
GeocodingBroadcastReceiver
类捕获消息
private class GeocodingBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(GeocodingService.ACTION_FINISHED)){
Log.i(LOG_TAG, "GeocodingService finished...");
Log.i(LOG_TAG, "Running The process has finished...");
}
}
}
最后,这是注册在onCreate()
方法中调用的BroadcastReceivers的代码
private void registerGeocodingReceiver(){
IntentFilter filter = new IntentFilter();
filter.addAction(GeocodingService.ACTION_FINISHED);
GeocodingBroadcastReceiver receiver = new GeocodingBroadcastReceiver();
getActivity().registerReceiver(receiver, filter);
}
private void startDistanceMatrixReceiver(){
Intent msgIntent = new Intent(getActivity(), DistanceMatrixService.class);
msgIntent.putExtra("iteraciones", 10);
getActivity().startService(msgIntent);
}
答案 0 :(得分:0)
问题是两个服务都有一个具有相同值的常量声明
GeocodingService.ACTION_FINISHED = "ACTION_FINISHED";
DistanceMatrixService.ACTION_FINISHED = "ACTION_FINISHED";
为每个人分配了不同的值。
GeocodingService.ACTION_FINISHED = "GEOCODING_FINISHED";
DistanceMatrixService.ACTION_FINISHED = "DISTANCE_MATRIX_FINISHED";
再次感谢给出正确答案的用户!