Android - Get Location Only One Time
i need to get th开发者_JAVA技巧e current user location on an android app, so i've read some tutorials and samples on the web, but i see that in all the examples, the location is retrived from a "onLocationChange" that mean that every time the location change, the code in the "onLocationChange" is executed.
i need only to get the user location at the moment the app is started.
Thanks for your help!
You can get the last know location using the code below. It gets the location providers and loops over the array backwards. i.e starts with GPS, if no GPS then gets network location. You can call this method whenever you need to get the location.
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
You can do this with LocationManager.getLastKnownLocation
use LocationManager.requestSingleUpdate()
look at: http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate%28java.lang.String,%20android.location.LocationListener,%20android.os.Looper%29
Put this in the main activity:
boolean bFirst = true;
function void onCreate(blabla) {
if(bFirst) {
//Do your stuff to get the location
bFirst = false;
}
}
Put the same in the onResume();
For the getGPS()
method which Phobos proposed to work properly you have to allow access within your AndroidManifest.xml
That will get rid of your error of receiving 0.0
Add these lines:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />
You can get the best location from all providers using this method. First create a class which is called FetchLocation
public class FetchLocation implements LocationListener {
private OnLocationFetchListner onLocationFetchListner;
private LocationManager locationManager;
public void setOnLocationFetchListner(OnLocationFetchListner fetchListner, Context context) {
this.onLocationFetchListner = fetchListner;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
onLocationFetchListner.OnFailed("PERMISSION DENIED");
return;
}
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Log.d("TAG", "BestProvider: " + provider);
if (provider != null) {
locationManager.requestLocationUpdates(provider, 60 * 1000, 100, this, Looper.getMainLooper());
} else {
onLocationFetchListner.OnFailed("NO PROVIDERS");
}
}
@Override
public void onLocationChanged(@NonNull Location location) {
if (location != null) {
onLocationFetchListner.OnComplete(location);
locationManager.removeUpdates(this);
locationManager = null;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
}
Then you create a OnLocationFetchListner interface!
public interface OnLocationFetchListner {
void OnComplete(Location location);
void OnFailed(String e);
}
Then you can call this method anywhere in your code!
FetchLocation fetchLocation = new FetchLocation();
fetchLocation.setOnLocationFetchListner(new OnLocationFetchListner() {
@Override
public void OnComplete(Location location) {
//do your work here
}
}
@Override
public void OnFailed(String e) {
Log.d("TAG", "OnFailed: "+e);
}
}, context);
And the you have to add this in the AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
精彩评论