LocationServices null

1 resposta Resolvido
android
denisspitfire

Bundle LocationServices esta vindo null logo após eu fazer este metodo. No metodo onConnected

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_localiza);
    txtlatitude = (TextView) findViewById(R.id.txtLatitude);
    callConnection();
}

private synchronized void callConnection() {

    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }


}

@Override
public void onConnected(Bundle connectionHint) {

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (l != null) {
            latitude = String.valueOf(l.getLatitude());
            longitude = String.valueOf(l.getLongitude());
            txtlatitude.setText(latitude);
            Log.i("latitude",latitude);
        }
    }else{
        Toast.makeText(Localiza.this,"Implementar",Toast.LENGTH_LONG).show();
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.i("LOG","onConnectionSuspended("+i+")");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.i("LOG","onConnectionSuspended()");
}

1 Resposta

denisspitfire
Solucao aceita

o correto é

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
}
}else{

Toast.makeText(Localiza.this,“Implementar”,Toast.LENGTH_LONG).show();

}

Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (l != null) {

latitude = String.valueOf(l.getLatitude());

longitude = String.valueOf(l.getLongitude());

txtlatitude.setText(latitude);

Log.i(“latitude”,latitude);

}
Criado 10 de abril de 2017
Ultima resposta 10 de abr. de 2017
Respostas 1
Participantes 1