从服务中检索位置

时间:2014-04-02 15:55:43

标签: java android service location location-client

我试图提供服务,听取用户的位置。

代码如下:

public class ServiceBeezer extends Service implements
    OnConnectionFailedListener, ConnectionCallbacks {

    private LocationRequest mLocationRequest;
    private LocationClient mLocationClient;

    public ServiceBeezer() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // 1. init locationrequest
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
        mLocationRequest.setFastestInterval(1000);

        // 2. mlocationclient
        mLocationClient = new LocationClient(this, this, this);

        return START_STICKY;
    }

    @Override
    public void onConnected(Bundle arg0) {
        Toast.makeText(this,
                getClass().getSimpleName() + "onConnected: " + arg0,
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(this, getClass().getSimpleName() + "onDisconnected: ",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        Toast.makeText(this,
                getClass().getSimpleName() + "onConnectionFailed: " + arg0,
                Toast.LENGTH_LONG).show();
    }

}

但永远不会调用onConnected()onDisconnected()onConnectionFailed()

我做错了什么?

我在Activity上有其他LocationRequest和LocationClient。

这可能是问题吗?

1 个答案:

答案 0 :(得分:1)

如果我没有错,你没有在上面粘贴的代码中的任何地方调用mLocationClient.connect()。