在服务中使用GoogleApiClient [英] Using GoogleApiClient in a service

查看:259
本文介绍了在服务中使用GoogleApiClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取服务中的位置更新信息.为此,我创建了一个服务,并在服务类的onStartCommand中创建了GoogleApiClient,但没有在该服务中获得连接回调.请帮助解决此问题.

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.

下面是服务代码,在活动中,我已使用startService方法启动服务:

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

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

服务代码

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;
}

}

推荐答案

主要问题:您永远不会拨打

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

此外,onStartService()可以被多次调用(即,每次调用startService()时-相反,您应将其移至onCreate()中以供Service使用,以确保在Service的生命周期中仅被调用一次)类似地,您应该

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.

这篇关于在服务中使用GoogleApiClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆