0

我正在尝试做一项服务,听用户的位置。

代码如下:

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()andonConnectionFailed()永远不会被调用。

我做错了什么?

我在 Activity 上有其他 LocationRequest 和 LocationClient。

可能是这个问题吗?

4

1 回答 1

1

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

于 2014-09-02T11:13:50.807 回答