Using GoogleApiClient in a service

吃可爱长大的小学妹 提交于 2019-12-18 12:19:14

问题


I am trying to get location update information in service. For this I have created a service and in onStartCommand of service class I am creating GoogleApiClient, but I am not getting connection call back in the service. Please help to resolve this.

Below given is the service code and in activity I have started service using startService method :

startService(new Intent(this, LocationService.class));

service code

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
                    GoogleApiClient.OnConnectionFailedListener, LocationListener {

private GoogleApiClient mLocationClient;

private Location mCurrentLocation;
LocationRequest mLocationRequest;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO do something useful
    Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show();
    mLocationClient = new GoogleApiClient.Builder(LocationService.this)
    .addApi(LocationServices.API).addConnectionCallbacks(LocationService.this)
    .addOnConnectionFailedListener(LocationService.this).build();

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mLocationRequest.setFastestInterval(1000);
    return Service.START_NOT_STICKY;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    mCurrentLocation = location;
    Toast.makeText(this, mCurrentLocation.getLatitude() +", "+ mCurrentLocation.getLatitude(), Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Connection failed", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
    //if(servicesConnected()) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
    //}

}

@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Disconnected. Please re-connect.",
            Toast.LENGTH_SHORT).show();
}



@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}


回答1:


The main issue: you never call mLocationClient.connect() so the connection process never starts.

Also, onStartService() can be called multiple times (i.e., every time startService() is called - instead, you should move this to onCreate() for your Service to ensure it is only called once during the lifecycle of your Service. Similarly, you should removeLocationUpdates() in onDestroy() and disconnect your mLocationClient.



来源:https://stackoverflow.com/questions/29712244/using-googleapiclient-in-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!