我的控制流位于IntentService
(由GcmListenerService触发),现在应该获取用户的位置。正如
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)
可能会返回null
,我应该请求一些位置更新。由于GPS机制,我认为每次更新时位置都比以前更准确。我想五次测量/更新应该足以准确定位。
如何在IntentService
中实现该逻辑?该类实现了侦听器接口:
public class LocationIntentService extends IntentService
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
这样我可以在public void onLocationChanged(Location location)
中使用计数器,并在五次更新后调用LocationServices.FusedLocationApi.removeLocationUpdates()
。但是,我不确定我是否可以信任Android,IntentService
只有onHandleIntent
长时间存在并且垃圾收集器不会被public class LocationIntentService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient;
public LocationIntentService() {
super("LocationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
System.out.println(result.toString());
}
@Override
public void onConnected(Bundle connectionHint) {
LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(500);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int cause) {
}
private void displayLocation(Location location) {
System.out.println(location.toString());
DbHandler dbHandler = new DbHandler(getBaseContext());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
double altitude = location.getAltitude();
float speed = location.getSpeed();
long time = location.getTime();
float accuracy = location.getAccuracy();
PersistedLocation persistedLocation = new PersistedLocation(time, latitude, longitude, altitude, accuracy, speed);
dbHandler.insertLocation(persistedLocation);
}
@Override
public void onLocationChanged(Location location) {
displayLocation(location);
}
}
删除。
这是我目前使用的完整代码,没有仅收集五个更新的逻辑:
A B C 1e+03 1e-03 3.39e+03
G H february
E 2.834967e+02 798
j 0.000000e+00
答案 0 :(得分:0)
我设法通过setNumUpdates(int i)
和setExpirationDuration(long l)
完成了此操作。
@Override
public void onConnected(Bundle connectionHint) {
LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(500);
mLocationRequest.setNumUpdates(5);
mLocationRequest.setExpirationDuration(10000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}