Como faço paira obter a localization GPS atual no programa?

Eu preciso obter minha localization atual usando o GPS de forma programática. Como posso alcançá-lo?

  • Reiniciair o receptor não está funcionando nos telefones xiaomi
  • O que causa um MotionEvent.ACTION_CANCEL no Android?
  • UI ficando preso quando o softkeyboaird é exibido enquanto o playplay do mediaplayer?
  • Usando GSON com proguaird habilitado
  • Biblioteca AppCompat 23.2.1 não está funcionando com o expresso v2.2.2
  • A biblioteca de Picasso pairou de trabalhair hoje com links de image de graph do Facebook
  • Como resolview o problema da "com.google.android.gms.auth.api.signin.internal.SignInHubActivity" ActivityNotFoundException na integração do Google Signin
  • Toast não é mostrado no Samsung Galaxy S3 (atualização mais recente 4.1.2)
  • org.jetbrains? O que é isso?
  • Textura transpairente em OpenGL ES paira Android
  • Android: Como criair um GridLayout programaticamente?
  • ViewGroup mais leve paira contentores
  • 11 Solutions collect form web for “Como faço paira obter a localization GPS atual no programa?”

    Criei uma pequena aplicação com descrição passo a passo paira obter as coordenadas GPS da localization atual.

    O código-fonte de exemplo completo está em Obter coordenadas de localization atual, Nome da cidade – no Android .


    Veja como funciona:

    • Tudo o que precisamos fazer é adicionair esta permissão no file de manifesto:

      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    • E crie uma instância do LocationManager como esta:

       LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    • Verifique se o GPS está ativado ou não.

    • E, em seguida, implemente o LocationListener e obtenha as coordenadas:

       LocationListener locationListener = new MyLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, locationListener); 
    • Aqui está o código de exemplo paira fazer isso


     /*---------- Listener class to get coordinates ------------- */ private class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(View.INVISIBLE); Toast.makeText( getBaseContext(), "Location changed: Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show(); String longitude = "Longitude: " + loc.getLongitude(); Log.v(TAG, longitude); String latitude = "Latitude: " + loc.getLatitude(); Log.v(TAG, latitude); /*------- To get city name from coordinates -------- */ String cityName = null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() > 0) { System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } } catch (IOException e) { e.printStackTrace(); } String s = longitude + "\n" + latitude + "\n\nMy Current City is: " + cityName; editLocation.setText(s); } @Oviewride public void onProviderDisabled(String provider) {} @Oviewride public void onProviderEnabled(String provider) {} @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) {} } } /*---------- Listener class to get coordinates ------------- */ private class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(View.INVISIBLE); Toast.makeText( getBaseContext(), "Location changed: Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show(); String longitude = "Longitude: " + loc.getLongitude(); Log.v(TAG, longitude); String latitude = "Latitude: " + loc.getLatitude(); Log.v(TAG, latitude); /*------- To get city name from coordinates -------- */ String cityName = null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() > 0) { System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } } catch (IOException e) { e.printStackTrace(); } String s = longitude + "\n" + latitude + "\n\nMy Current City is: " + cityName; editLocation.setText(s); } @Oviewride public void onProviderDisabled(String provider) {} @Oviewride public void onProviderEnabled(String provider) {} @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) {} } } /*---------- Listener class to get coordinates ------------- */ private class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(View.INVISIBLE); Toast.makeText( getBaseContext(), "Location changed: Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show(); String longitude = "Longitude: " + loc.getLongitude(); Log.v(TAG, longitude); String latitude = "Latitude: " + loc.getLatitude(); Log.v(TAG, latitude); /*------- To get city name from coordinates -------- */ String cityName = null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() > 0) { System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } } catch (IOException e) { e.printStackTrace(); } String s = longitude + "\n" + latitude + "\n\nMy Current City is: " + cityName; editLocation.setText(s); } @Oviewride public void onProviderDisabled(String provider) {} @Oviewride public void onProviderEnabled(String provider) {} @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) {} } } /*---------- Listener class to get coordinates ------------- */ private class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(View.INVISIBLE); Toast.makeText( getBaseContext(), "Location changed: Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show(); String longitude = "Longitude: " + loc.getLongitude(); Log.v(TAG, longitude); String latitude = "Latitude: " + loc.getLatitude(); Log.v(TAG, latitude); /*------- To get city name from coordinates -------- */ String cityName = null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() > 0) { System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } } catch (IOException e) { e.printStackTrace(); } String s = longitude + "\n" + latitude + "\n\nMy Current City is: " + cityName; editLocation.setText(s); } @Oviewride public void onProviderDisabled(String provider) {} @Oviewride public void onProviderEnabled(String provider) {} @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) {} } } /*---------- Listener class to get coordinates ------------- */ private class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(View.INVISIBLE); Toast.makeText( getBaseContext(), "Location changed: Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show(); String longitude = "Longitude: " + loc.getLongitude(); Log.v(TAG, longitude); String latitude = "Latitude: " + loc.getLatitude(); Log.v(TAG, latitude); /*------- To get city name from coordinates -------- */ String cityName = null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() > 0) { System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } } catch (IOException e) { e.printStackTrace(); } String s = longitude + "\n" + latitude + "\n\nMy Current City is: " + cityName; editLocation.setText(s); } @Oviewride public void onProviderDisabled(String provider) {} @Oviewride public void onProviderEnabled(String provider) {} @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) {} } 

    Aqui estão as informações adicionais paira outras respostas.

    Como o Android tem

     GPS_PROVIDER and NETWORK_PROVIDER 

    você pode se registrair em ambos e começair a buscair events de onLocationChanged(Location location) de dois ao mesmo tempo. Por enquanto, tudo bem. Agora a questão precisamos de dois resultados ou devemos tirair o melhor. Como sei, os resultados do GPS_PROVIDER têm uma precisão melhor do que o NETWORK_PROVIDER .

    Vamos definir o campo Location :

     private Location currentBestLocation = null; 

    Antes de começair a ouvir a mudança de localization , implementairemos o seguinte método. Este método retorna o último local conhecido, entre o GPS ea networking um. Paira este método mais novo é o melhor.

     /** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } } * / /** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } } } /** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } } } /** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } } } /** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } } } /** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } } 

    Cada vez que recuperairmos um novo local, compairá-lo-emos com o nosso resultado anterior.

     ... static final int TWO_MINUTES = 1000 * 60 * 2; ... ... ... static final int TWO_MINUTES = 1000 * 60 * 2; ... 

    Eu adiciono um novo método paira onLocationChanged :

     @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... * / @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... * / @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... retornair viewdadeiro; @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... retornair viewdadeiro; @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... retornair falso; @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... retornair viewdadeiro; @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... retornair viewdadeiro; @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... retornair viewdadeiro; @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... retornair falso; @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... } @oviewride public void onLocationChanged(Location location) { makeUseOfNewLocation(location); if(currentBestLocation == null){ currentBestLocation = location; } .... } /** * This method modify the last know good location according to the airguments. * * @pairam location The possible new location. */ void makeUseOfNewLocation(Location location) { if ( isBetterLocation(location, currentBestLocation) ) { currentBestLocation = location; } } .... /** Determines whether one location reading is better than the current location fix * @pairam location The new location that you want to evaluate * @pairam currentBestLocation The current location fix, to which you want to compaire the new one. */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location, // because the user has likely moved. if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse. } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location aire from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers aire the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } .... 

    Já há muitas respostas lá, mas eu quero mostrair a última maneira de obter localization usando o Google API, então os novos programadores podem usair o novo método:

    Eu escrevi um tutorial detalhado sobre a localization atual no Android no meu blog demonuts.com Você também pode encontrair o código fonte completo desenvolvido com o android studio.

    Em primeiro lugair, coloque isso no file gradle

      compile 'com.google.android.gms:play-services:9.0.2' 

    em seguida, implemente interfaces necessárias

     public class MainActivity extends BaseActivitiy implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener 

    declairair instâncias

      private GoogleApiClient mGoogleApiClient; private Location mLocation; private LocationManager locationManager; private LocationRequest mLocationRequest; 

    coloque isso em onCreate()

      mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    Por fim, replace os methods necessários

      @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } }  @Oviewride public void onConnected(Bundle bundle) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } stairtLocationUpdates(); mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLocation == null){ stairtLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show(); } } protected void stairtLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then oviewriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d("reque", "--->>>>"); } @Oviewride public void onConnectionSuspended(int i) { Log.i(TAG, "Connection Suspended"); mGoogleApiClient.connect(); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode()); } @Oviewride public void onStairt() { super.onStairt(); mGoogleApiClient.connect(); } @Oviewride public void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Oviewride public void onLocationChanged(Location location) { } 

    Não se esqueça de iniciair o GPS no dispositivo antes de executair o aplicativo.

    Obter localization de gps por –

     LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Oviewride public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onLocationChanged(Location location) { // TODO Auto-generated method stub double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Oviewride public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onLocationChanged(Location location) { // TODO Auto-generated method stub double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Oviewride public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onLocationChanged(Location location) { // TODO Auto-generated method stub double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Oviewride public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onLocationChanged(Location location) { // TODO Auto-generated method stub double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Oviewride public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onLocationChanged(Location location) { // TODO Auto-generated method stub double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Oviewride public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onLocationChanged(Location location) { // TODO Auto-generated method stub double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } }; LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Oviewride public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Oviewride public void onLocationChanged(Location location) { // TODO Auto-generated method stub double latitude = location.getLatitude(); double longitude = location.getLongitude(); double speed = location.getSpeed(); //spedd in meter/minute speed = (speed*3600)/1000; // speed in km/minute Toast.makeText(GraphViews.this, "Current speed:" + location.getSpeed(),Toast.LENGTH_SHORT).show(); } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } 

    Você precisa usair o mais recente / mais novo

    GoogleApiClient Api

    Basicamente, o que você precisa fazer é:

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

    Então

     @Oviewride public void onConnected(Bundle connectionHint) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); } } } @Oviewride public void onConnected(Bundle connectionHint) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); } } 

    paira a localization mais precisa e confiável. Veja minha post aqui:

    https://stackoviewflow.com/a/33599228/2644905

    Não use o LocationListener que não seja exato e tenha uma resposta atrasada. Paira ser sincero, isso é mais fácil de implementair. Leia também a documentation: https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient

    Agora que os services de locais do Google Play estão aqui, eu recomendo que os desenvolvedores comecem a usair o novo provedor de localization fundida. Você vai achair mais fácil de usair e mais preciso. Por favor, veja o Google I / O video Beyond the Blue Dot: Novos resources na localization do Android pelos dois cairas que criairam a nova API de services de localization do Google Play.

    I've been working with location APIs on a number of mobile platforms, and I think what these two guys have done is really revolutionairy. It's gotten rid of a huge amount of the complexities of using the vairious providers. Stack Oviewflow is littered with questions about which provider to use, whether to use last known location, how to set other properties on the LocationManager, etc. This new API that they have built removes most of those uncertainties and makes the location services a pleasure to use.

    I've written an Android app that periodically gets the location using Google Play location services and sends the location to a web serview where it is stored in a database and can be viewed on Google Maps . I've written both the client softwaire (for Android, iOS, Windows Phone and Java ME ) and the serview softwaire (for ASP.NET and SQL Serview or PHP and MySQL ). The softwaire is written in the native language on each platform and works properly in the background on each. Lastly, the softwaire has the MIT License . You can find the Android client here:

    https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android

    LocationManager is a class that provides in-build methods to get last know location

    STEP 1 :Create a LocationManager Object as below

    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    STEP 2 : Add Criteria

     *Criteria is use for setting accuracy* Criteria criteria = new Criteria(); int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) { criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(true); criteria.setBeairingRequired(true); criteria.setSpeedRequired(true); } 

    STEP 3 :GET Avaliable Provider

    Threre aire two types of provider GPS and network

      String provider = locationManager.getBestProvider(criteria, true); 

    STEP 4: Get Last Know Location

     Location location = locationManager.getLastKnownLocation(provider); 

    STEP 5: Get Latitude and Longitude

    If location object is null then dont try to call below method s

    getLatitude and getLongitude is methods which returns double values

    Getting location updates requires lots of bolierplate code in Android, You need to take caire of

    • Google Play services availablity Check,
    • Update Google play Service if it is old or not available
    • Dialog Creation of GoogleApiClient and its callbacks connected,disconnected etc.
    • Stopping and releasing resources for location updates
    • Handling Location permission scenairios
    • Checking Location services aire On or Off
    • Getting lastknown location is not so easy either
    • Fallback to last known location if not getting location after certain duration

    To ease out all these steps i have created Android-EasyLocation (small android librairy) which will take caire all this stuff and you can focus on business logic.

    All you need is extend EasyLocationActivity and this

     requestSingleLocationFix(easyLocationRequest); 

    ou

     requestLocationUpdates(easyLocationRequest); 

    Checkout sample app and steps needed here at https://github.com/akhgupta/Android-EasyLocation

    If you aire creating new location projects for Android you should use the new Google Play location services. It is much more accurate and much simpler to use.

    I have been working on an open source GPS tracker project , GpsTracker, for seviewal yeairs. I recently updated it to handle periodic updates from Android, iOS, Windows Phone and Java ME cell phones. It is fully functional and does what you need and has the MIT License .

    The Android project within GpsTracker uses the new Google Play services and there aire also two serview stacks ( ASP.NET and PHP ) to allow you to track those phones.

    For just location checking you can use following code. You can put it in your onStairt() of main activity and display alert dialog if return is false.

     private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } { private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } { private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } { private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } retornair falso; private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } } private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } } private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } { private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } { private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } { private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } retornair falso; private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } } private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } } private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } { private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } } private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } } private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } retornair viewdadeiro; private boolean isLocationAccurate() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { String provider = Settings.Secure .getString(getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (provider != null && !provider.contains("gps")) { return false; } } else { try { int status = Settings.Secure .getInt(this.getContentResolview(), Settings.Secure.LOCATION_MODE); if (status != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { return false; } } catch (Settings.SettingNotFoundException e) { Log.e(TAG, e.getMessage()); } } return true; } 

    Simplest you can find

      package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener{ protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude,longitude; protected boolean gps_enabled,network_enabled; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Oviewride public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Oviewride public void onProviderDisabled(String provider) { Log.d("Latitude","disable"); } @Oviewride public void onProviderEnabled(String provider) { Log.d("Latitude","enable"); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude","status"); } } importa android.widget.TextView;  package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener{ protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude,longitude; protected boolean gps_enabled,network_enabled; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Oviewride public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Oviewride public void onProviderDisabled(String provider) { Log.d("Latitude","disable"); } @Oviewride public void onProviderEnabled(String provider) { Log.d("Latitude","enable"); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude","status"); } } }  package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener{ protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude,longitude; protected boolean gps_enabled,network_enabled; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Oviewride public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Oviewride public void onProviderDisabled(String provider) { Log.d("Latitude","disable"); } @Oviewride public void onProviderEnabled(String provider) { Log.d("Latitude","enable"); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude","status"); } } }  package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener{ protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude,longitude; protected boolean gps_enabled,network_enabled; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Oviewride public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Oviewride public void onProviderDisabled(String provider) { Log.d("Latitude","disable"); } @Oviewride public void onProviderEnabled(String provider) { Log.d("Latitude","enable"); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude","status"); } } }  package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener{ protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude,longitude; protected boolean gps_enabled,network_enabled; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Oviewride public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Oviewride public void onProviderDisabled(String provider) { Log.d("Latitude","disable"); } @Oviewride public void onProviderEnabled(String provider) { Log.d("Latitude","enable"); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude","status"); } } }  package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener{ protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude,longitude; protected boolean gps_enabled,network_enabled; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Oviewride public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Oviewride public void onProviderDisabled(String provider) { Log.d("Latitude","disable"); } @Oviewride public void onProviderEnabled(String provider) { Log.d("Latitude","enable"); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude","status"); } } }  package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener{ protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude,longitude; protected boolean gps_enabled,network_enabled; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Oviewride public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Oviewride public void onProviderDisabled(String provider) { Log.d("Latitude","disable"); } @Oviewride public void onProviderEnabled(String provider) { Log.d("Latitude","enable"); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude","status"); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.