Back to project page android-location-api.
The source code is released under:
MIT License
If you think the Android project android-location-api listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.devahoy.androidlocation; /*from w w w. j av a2 s . c o m*/ import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private final String TAG = MainActivity.class.getSimpleName(); private GoogleApiClient mClient; private LocationRequest mLocationRequest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); LocationManager locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE); // Enable Location. if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } } @Override protected void onStart() { super.onStart(); if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { mClient.connect(); } } @Override protected void onStop() { mClient.disconnect(); super.onStop(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private boolean checkServiceAvailable() { int resultCode = GooglePlayServicesUtil. isGooglePlayServicesAvailable(this); return ConnectionResult.SUCCESS == resultCode; } @Override public void onConnected(Bundle bundle) { Log.d(TAG, "onConnected"); // Get Location without update. // LocationServices.FusedLocationApi.getLastLocation(mClient); mLocationRequest = LocationRequest.create(); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); mLocationRequest.setInterval(5000); LocationServices.FusedLocationApi.requestLocationUpdates( mClient, mLocationRequest, this); } @Override public void onConnectionSuspended(int i) { Log.d(TAG, "onConnectionSuspended"); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { Log.d(TAG, "onConnectionFailed"); } @Override public void onLocationChanged(Location location) { Toast.makeText(this, "Location : " + location.toString(), Toast.LENGTH_SHORT).show(); } }