我已实施了一项后台服务来接收位置更新:

public class TestActivity extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static String TAG = TestActivity_backup.class.getName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

private LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient=null;


@Override
public void onCreate() {
    super.onCreate();
    if (!isGooglePlayServicesAvailable()) {
        stopSelf();
    }
    setContentView(R.layout.activity_test);

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10000).setFastestInterval(5000).setSmallestDisplacement(20);
    this.buildGoogleApiClient();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    this.destination = intent.getStringExtra("DESTINATION");
    LatLng latLng = intent.getParcelableExtra("LOCATION");
    this.location = new Location("");
    this.location.setLatitude(latLng.latitude);
    this.location.setLongitude(latLng.longitude);

    return START_NOT_STICKY;
}

@Override
public void onLocationChanged(Location location){
    Toast.makeText(TestActivity.this, "User location changed", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnected(Bundle bundle) {
    Toast.makeText(TestActivity.this, "Location  service connected", Toast.LENGTH_SHORT).show();
}


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

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "Location services suspended. Please reconnect.");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    //TODO
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}


private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        //GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
        return false;
    }
}


}

我已经实现了所有相关方法.onCreate和onStartCommand被调用但是onConnec
永远不会调用ted和onLocationChanged。如果我实现了一个用于位置更新的活动,那么它的工作就很好了。
我在这里想念什么?

最佳答案

不要忘记根据upon Accuracy添加权限ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION

并使用此代码

public class LocationService extends Service {

    private LocationListener locationListener;

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

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        super.onStartCommand(intent, flags, startId);
        return Service.START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        this.locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this.locationListener);
    }

    private static class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(final Location location) {
        }

        @Override
        public void onProviderDisabled(final String provider) {
        }

        @Override
        public void onProviderEnabled(final String provider) {
        }

        @Override
        public void onStatusChanged(final String provider, final int status, final Bundle extras) {
        }

    }

}

10-07 19:25