我正在开发一个由服务和活动组成的应用程序。该服务使用 LocationClient 对象每分钟请求一次当前位置。该活动使用另一个 LocationClient 对象在单击按钮后请求单个当前位置。
在服务中:
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(60000);
mLocationRequest.setFastestInterval(60000);
mLocationClient = new LocationClient(this, this, this);
// ... when connected:
mLocationClient.requestLocationUpdates(mLocationRequest, this);
在活动中:
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(0); // 0 should be ok since only a single location is requested
mLocationRequest.setFastestInterval(0);
mLocationRequest.setNumUpdates(1); // get only a single location
mLocationClient = new LocationClient(this, this, this);
// ... when connected:
mLocationClient.requestLocationUpdates(mLocationRequest, this);
仅这项服务就可以按预期工作。
但是,如果服务已启动并且活动尝试获取单个当前位置,那么它会在服务接收到更新的位置之前接收到该位置。所以活动必须等待长达 60 秒。
但是,如果服务未启动并且活动尝试获取单个当前位置,那么它会在预期的短时间内(通常 < 5 秒)后接收位置。
是什么导致了问题?每个应用程序只允许一个 LocationListener 吗?