0
サービスから位置を更新しようとしていますが、接続するためにgoogleApiClientを呼び出すと、onConnected、onConnectionSuspended、onConnectionFailedなどのメソッドが呼び出されません。GoogleApiClientはサービス内で接続されたメソッドを呼び出さない
はここで完全なコードです:
public abstract class BASE_LOCAT_SERVICE extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
public String TAG="LOC_SERVICE";
public GoogleApiClient googleApiClient;
private Location lastLocation;
private static int UPDATE_INTERVAL = 5000;
private static int DISPLACEMENT = 10;
private LocationRequest locationRequest;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG+"_LOC","On create Command");
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
Log.i(TAG+"_LOC","googleApiClient Connecting");
}
protected void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setSmallestDisplacement(DISPLACEMENT);
Log.i(TAG+"_LOC","createLocationRequest");
}
private void initialiseWithLastLocation() {
try{
lastLocation = LocationServices.FusedLocationApi.getLastLocation(
googleApiClient);
Log.i(TAG+"_LOC","initialiseWithLastLocation Initialised");
}catch(SecurityException ex){
Log.e(TAG+"_LOC", "Unable to get last known location", ex);
}
}
protected void startLocationUpdates() {
try {
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
Log.i(TAG+"_LOC","startLocationUpdates Initialised");
} catch (SecurityException ex) {
Log.e(TAG+"_LOC", "Unable to get Last Known location", ex);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
createLocationRequest();
initialiseWithLastLocation();
startLocationUpdates();
Log.i(TAG+"_LOC","googleApiClient Connected");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG+"_LOC","googleApiClient onConnectionSuspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i(TAG+"_LOC","googleApiClient onConnectionFailed");
}
@Override
public void onLocationChanged(Location location) {
lastLocation = location;
Log.i(TAG+"_LOC"," onLocationChanged");
}
public String getLastLocation()
{
if(lastLocation!=null)
{
String ret="Location : \n";
ret = ret+"Latitude : "+lastLocation.getLatitude()+"\n";
ret = ret+"Longitude : "+lastLocation.getLongitude()+"\n";
ret = ret+"Altitude : "+lastLocation.getAltitude()+"\n";
ret = ret+"Accuracy : "+lastLocation.getAccuracy()+"\n";
ret = ret+"Provider : "+lastLocation.getProvider()+"\n";
ret = ret+"Speed : "+lastLocation.getSpeed()+"\n";
ret = ret+"MAP Link : https://www.google.com/maps/?q="+lastLocation.getLatitude()+","+lastLocation.getLongitude()+"\n";
return ret;
}
else
return "Location Not Available";
}
}
とlogcat
が表示さだけこの:
LOC_SERVICE_LOC: On create Command
LOC_SERVICE_LOC: googleApiClient Connecting
onStartCommand
内部を使用して接続する必要がありますああすごい!私に思い出させてくれてありがとう!どのように私はその部分を見逃すことができます。ありがとう –