Intento crear un ejemplo simple, solo que obtenga una vez la posición GPS pero usando Google play Services, es decir haciendo uso de FusedLocationProviderApi
Tengo lo siguiente:
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Log.d(TAG, "checkIfLocationEnabled: " + checkIfLocationEnabled(getBaseContext()));
}
});
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.enableAutoManage(this, this)
.build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
this.startActivity(myIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.i(TAG, "onConnected: ");
}
@Override
public void onConnectionSuspended(int i) {
Log.w(TAG, "onConnectionSuspended: ");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed: " + connectionResult.toString());
}
@Override
protected void onStart() {
Log.i(TAG, "onStart: ");
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
Log.i(TAG, "onStop: ");
super.onStop();
mGoogleApiClient.disconnect();
}
@Override
protected void onDestroy() {
Log.i(TAG, "onDestroy: ");
super.onDestroy();
mGoogleApiClient.disconnect();
}
}
Todo lo que voy encontrado por la red, es usando IntentService
a mi me interesa solo obtener una vez, no tener un servicio todo el rato obteniendo posiciones en background.
Es decir al pulsar el fabButton
que se obtenga lat, lon, altitud, bearing, velocidad etc..