【发布时间】:2011-10-15 12:52:04
【问题描述】:
在我的代码中,我使用IntentService 来收听位置更新(GPS 或网络更新),当收到事件时会触发此IntentService,因此它从任何活动中以startService() 开始.
public class AddLocationService extends IntentService implements LocationListener {
/*My code here*/
}
@Override
protected void onHandleIntent(Intent intent) {
if(getOldLoc() == null)
{
//Get a new location
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_INTERVAL_GPS, 0, this);
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_INTERVAL_GPS, 0, this);
Log.d(AddLocationService.TAG, "Network listener started");
this.time_start_listening = System.currentTimeMillis();
mTimerThread mTimerRunnable = new mTimerThread();
this.timerThread = new Thread(mTimerRunnable);
this.timerThread.start();
}
else
/*REUSE OLD LOCATION*/
}
现在我的问题是:当两个事件启动 IntentService 并且第二个事件启动它而第一个事件仍在请求更新时,我希望第二个事件等到第一个事件完全完成(找到位置或计时器线程完成)。
但是,每当第二次执行 IntentService 时(第一个实例仍在运行),它会打印日志并按照并行执行的方式执行。
但是我认为IntentService 的主要目标是它是连续的,所以第二个意图必须等到第一个意图完成...
我是不是误会了什么?
【问题讨论】:
标签: android service android-intent intentservice