특정 기간 동안 위치 업데이트를 수집하고 끝에 위치를 지정합니다. 나는 현재 방법 onLocationChanged
에 전달 된 시간을 추적합니다.나중에 onLocationChanged 외부에서 처리하기 위해 위치 업데이트를 수집하는 방법
public class LocationProvider implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private static final String TAG = LocationProvider.class.getSimpleName();
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private long mStartTime;
private long mDuration;
List<Location> locations;
public LocationProvider(Context context) {
mContext = context;
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
if (api.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS) {
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
} else {
Log.e(TAG, "Could not connect to Google Play services");
}
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed: " + connectionResult.toString());
}
@Override
public void onConnectionSuspended(int i) {
Log.e(TAG, "GoogleApiClient connection has been suspended: " + String.valueOf(i));
}
@Override
public void onLocationChanged(Location location) {
locations.add(location);
if (System.currentTimeMillis() - mStartTime > mDuration) {
stopTracking();
// do stuff
}
}
public void startTracking(long interval, long fastInterval, long duration) {
mDuration = duration;
locations = new ArrayList<>();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(interval)
.setFastestInterval(fastInterval);
if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
mStartTime = System.currentTimeMillis();
}
private void stopTracking() {
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
}
그러나이 논리를 이혼하고 싶습니다. 내 필요에 따라 위치를 달리하고 싶기 때문에. 내 위치 수신기를 등록하기 위해 새 스레드를 만든 경우 위치 수집이 완료되어 locations
목록을 사용할 때까지 주 스레드에서 대기 할 수 있다고 생각했습니다.
- 에 대해 호출하지만 긴
onLocationChanged
후gatherUpdates
수익률에 도달mLocationProvider.startTracking(interval, fastInterval, duration);
라인 :public class LocationUpdates { private LocationProvider mLocationProvider; private Looper mLooper; public List<Location> gatherUpdates(final Context context, final long interval, final long fastInterval, final long duration) throws InterruptedException { long startTime = System.currentTimeMillis(); new Thread() { @Override public void run() { Looper.prepare(); mLooper = Looper.myLooper(); mLocationProvider = new LocationProvider(context); mLocationProvider.startTracking(interval, fastInterval, duration); Looper.loop(); } }.start(); while (System.currentTimeMillis() - startTime < duration) { } mLooper.quit(); mLooper.getThread().join(); return mLocationProvider.locations; } }
대신, 내가 무엇을 관찰하는 것은 (간격 삼초, 기간 십초)이었다 처음 만 지금
그래서 전자 위치 청취자가 등록되었지만, 무언가가 명확하게 업데이트 수신을 차단합니다. 왜 내 논리가 내가 기대하는 바를하지 않는지 알 수 없다.
스레드가 없어도 많은 수의 위치 업데이트를 수집하고 컬렉션 완료 후 onLocationChanged
외부에서 작업하는 방법이 있습니까?
, 나는 잠재적으로 해결 할 수있다. 이 질문에 대한 대답은 나에게 여전히 중요합니다. – Reti43