If you think the Android project hubblog listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2012 Kevin Sawicki <kevinsawicki@gmail.com>
*/*www.java2s.com*/
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.github.kevinsawicki.wishlist;
importstatic android.content.Context.LOCATION_SERVICE;
importstatic android.location.Criteria.ACCURACY_FINE;
importstatic android.location.LocationManager.GPS_PROVIDER;
importstatic android.location.LocationManager.NETWORK_PROVIDER;
importstatic android.location.LocationManager.PASSIVE_PROVIDER;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import java.io.IOException;
import java.util.List;
/**
* Utilities for dealing with the location service
*/publicclass LocationUtils {
/**
* Get the location with the later date
*
* @param location1
* @param location2
* @return location
*/privatestatic Location getLatest(final Location location1,
final Location location2) {
if (location1 == null)
return location2;
if (location2 == null)
return location1;
if (location2.getTime() > location1.getTime())
return location2;
elsereturn location1;
}
/**
* Get the latest location trying multiple providers
* <p>
* Calling this method requires that your application's manifest contains the
* {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission
*
* @param context
* @return latest location set or null if none
*/publicstatic Location getLatestLocation(final Context context) {
LocationManager manager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(ACCURACY_FINE);
String provider = manager.getBestProvider(criteria, true);
Location bestLocation;
if (provider != null)
bestLocation = manager.getLastKnownLocation(provider);
else
bestLocation = null;
Location latestLocation = getLatest(bestLocation,
manager.getLastKnownLocation(GPS_PROVIDER));
latestLocation = getLatest(latestLocation,
manager.getLastKnownLocation(NETWORK_PROVIDER));
latestLocation = getLatest(latestLocation,
manager.getLastKnownLocation(PASSIVE_PROVIDER));
return latestLocation;
}
/**
* Get address for location
*
* @param context
* @param location
* @return possibly null address retrieved from location's latitude and
* longitude
*/publicstatic Address getAddress(final Context context,
final Location location) {
if (location == null)
return null;
final Geocoder geocoder = new Geocoder(context);
final List<Address> addresses;
try {
addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
} catch (IOException e) {
return null;
}
if (addresses != null && !addresses.isEmpty())
return addresses.get(0);
elsereturn null;
}
}