FusedLocationApi.getLastLocation返回null

时间:2017-07-28 15:15:22

标签: android

我有单身人士进行定位服务

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

    public static final String TAG = LocationService.class.getSimpleName();

    private static LocationService instance;
    private LocationRequest mLocationRequest;
    private GoogleApiClient mGoogleApiClient;
    public Location location;
    private Context context;

    private LocationService (Context context) {

        this.context = context;

        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        mGoogleApiClient.connect();

        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(60 * 1000); // 1 second, in milliseconds

    }

    public static LocationService getInstance(Context context){

        if (null == instance){
            instance = new LocationService(context);
        }
        return instance;
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.i(TAG, "Location services connected.");

        if (ActivityCompat.checkSelfPermission(this.context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        this.location = location;

        if (location != null) {
            Log.d(TAG, location.toString());
        }
    }

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

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }

    public void onLocationChanged(Location location) {

        this.location = location;

        Log.i(TAG, "Location changed.");
        Log.i(TAG, location.toString());

    }
}

我通过调用MainActivity

onCreate中启动它
LocationService locationService = LocationService.getInstance(getApplicationContext());

但我的位置始终为空,onLocationChanged不会被称为

正在调用

OnConnected,我在调试器中检查过,但location内部为空

我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果您的上一个位置为空,则需要注册以获得至少一次更新,以获得第一个位置。

Check the documentation 表示“请求位置更新”。

答案 1 :(得分:0)

尝试从设置中检查位置是否已启用:

private void checkLocationEnabled() {
    // check if location is enabled
    final LocationManager manager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        showSettingsAlert(mContext);
    }
}


public void showSettingsAlert(final Context context) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Location Settings");
    alertDialog.setMessage("Location is not enabled. Do you want to go to settings menu ?");
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}