本文整理汇总了Java中android.location.LocationManager.getLastKnownLocation方法的典型用法代码示例。如果您正苦于以下问题:Java LocationManager.getLastKnownLocation方法的具体用法?Java LocationManager.getLastKnownLocation怎么用?Java LocationManager.getLastKnownLocation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.location.LocationManager
的用法示例。
在下文中一共展示了LocationManager.getLastKnownLocation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: register
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* 注册
* <p>使用完记得调用{@link #unregister()}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
* <p>如果{@code minDistance}为0,则通过{@code minTime}来定时更新;</p>
* <p>{@code minDistance}不为0,则以{@code minDistance}为准;</p>
* <p>两者都为0,则随时刷新。</p>
*
* @param minTime 位置信息更新周期(单位:毫秒)
* @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米)
* @param listener 位置刷新的回调接口
* @return {@code true}: 初始化成功<br>{@code false}: 初始化失败
*/
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
if (listener == null) return false;
mLocationManager = (LocationManager) Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
mListener = listener;
if (!isLocationEnabled()) {
ToastUtils.showShortToastSafe("无法定位,请打开定位服务");
return false;
}
String provider = mLocationManager.getBestProvider(getCriteria(), true);
Location location = mLocationManager.getLastKnownLocation(provider);
if (location != null) listener.getLastKnownLocation(location);
if (myLocationListener == null) myLocationListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
return true;
}
示例2: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* PRECONDITION: The user has granted the system permission to access their device's location. May crash otherwise
* @return the user's last known location if known, or null otherwise
*/
@SuppressWarnings("MissingPermission")
public static Location getLocation(Activity activity) {
/**
* Reference:
* https://github.com/googlemaps/android-samples/blob/master/tutorials/CurrentPlaceDetailsOnMap
*/
LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
if (locationManager == null)
return null;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0.0f, listener);
final Location[] location = {null};
location[0] = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location[0];
}
示例3: getLastKnownLocation
import android.location.LocationManager; //导入方法依赖的package包/类
private Location getLastKnownLocation(){
if( ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ){
return null;
}
LocationManager locationManager =
(LocationManager) this.getSystemService( LOCATION_SERVICE );
List<String> providers = locationManager.getProviders( true );
Location bestLocation = null;
for( String provider : providers ){
Location l = locationManager.getLastKnownLocation( provider );
if( l == null ){
continue;
}
if( bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy() ){
bestLocation = l; // Found best last known location;
}
}
return bestLocation;
}
示例4: register
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* 注册
* <p>使用完记得调用{@link #unregister()}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
* <p>如果{@code minDistance}为0,则通过{@code minTime}来定时更新;</p>
* <p>{@code minDistance}不为0,则以{@code minDistance}为准;</p>
* <p>两者都为0,则随时刷新。</p>
*
* @param minTime 位置信息更新周期(单位:毫秒)
* @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米)
* @param listener 位置刷新的回调接口
* @return {@code true}: 初始化成功<br>{@code false}: 初始化失败
*/
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
if (listener == null) return false;
mLocationManager = (LocationManager) Utils.getContext().getSystemService(LOCATION_SERVICE);
mListener = listener;
if (!isLocationEnabled()) {
Log.d(TAG, "无法定位,请打开定位服务");
return false;
}
String provider = mLocationManager.getBestProvider(getCriteria(), true);
Location location = mLocationManager.getLastKnownLocation(provider);
if (location != null) listener.getLastKnownLocation(location);
if (myLocationListener == null) myLocationListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
return true;
}
示例5: getLastKnownLocation
import android.location.LocationManager; //导入方法依赖的package包/类
private void getLastKnownLocation() {
Context context = this.getContext();
Location gpsLocation = null;
Location networkLocation = null;
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (PermissionUtils.checkPermission(context, Manifest.permission.ACCESS_FINE_LOCATION))
gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (PermissionUtils.checkPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION))
networkLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location = betterLocation(gpsLocation, networkLocation);
if (location != null) this.output(new Geolocation(location));
this.finish();
}
示例6: getBestLastKnownLocation
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* Restituice la posizione più accurata e più recente del dispositivo, scelta tra tutti i location provider disponibili.
*/
public static Location getBestLastKnownLocation(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getAllProviders();
Location bestLocation = null;
for (String provider : providers) {
try {
Location location = locationManager.getLastKnownLocation(provider);
if (bestLocation == null || location != null
&& location.getElapsedRealtimeNanos() > bestLocation.getElapsedRealtimeNanos()
&& location.getAccuracy() > bestLocation.getAccuracy())
bestLocation = location;
} catch (SecurityException ignored) {
}
}
return bestLocation;
}
示例7: register
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* 注册
* <p>使用完记得调用{@link #unregister()}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
* <p>如果{@code minDistance}为0,则通过{@code minTime}来定时更新;</p>
* <p>{@code minDistance}不为0,则以{@code minDistance}为准;</p>
* <p>两者都为0,则随时刷新。</p>
*
* @param minTime 位置信息更新周期(单位:毫秒)
* @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米)
* @param listener 位置刷新的回调接口
* @return {@code true}: 初始化成功<br>{@code false}: 初始化失败
*/
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
if (listener == null) return false;
mLocationManager = (LocationManager) Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
mListener = listener;
if (!isLocationEnabled()) {
ToastUtils.showShortSafe("无法定位,请打开定位服务");
return false;
}
String provider = mLocationManager.getBestProvider(getCriteria(), true);
Location location = mLocationManager.getLastKnownLocation(provider);
if (location != null) listener.getLastKnownLocation(location);
if (myLocationListener == null) myLocationListener = new MyLocationListener();
mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
return true;
}
示例8: getCurrentPosition
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* Get the current position. This can return almost immediately if the location is cached or
* request an update, which might take a while.
*
* @param options map containing optional arguments: timeout (millis), maximumAge (millis) and
* highAccuracy (boolean)
*/
@ReactMethod
public void getCurrentPosition(
ReadableMap options,
final Callback success,
Callback error) {
LocationOptions locationOptions = LocationOptions.fromReactMap(options);
try {
LocationManager locationManager =
(LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
String provider = getValidProvider(locationManager, locationOptions.highAccuracy);
if (provider == null) {
error.invoke(PositionError.buildError(
PositionError.PERMISSION_DENIED,
"No location provider available."));
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null &&
SystemClock.currentTimeMillis() - location.getTime() < locationOptions.maximumAge) {
success.invoke(locationToMap(location));
return;
}
new SingleUpdateRequest(locationManager, provider, locationOptions.timeout, success, error)
.invoke();
} catch (SecurityException e) {
throwLocationPermissionMissing(e);
}
}
示例9: addLocationListener
import android.location.LocationManager; //导入方法依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void addLocationListener() {
// Note: Use the Fused Location Provider from Google Play Services instead.
// https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
mLocationManager =
(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener);
Log.d("BoundLocationMgr", "Listener added");
// Force an update with the last location, if available.
Location lastLocation = mLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
if (lastLocation != null) {
mListener.onLocationChanged(lastLocation);
}
}
示例10: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Permission not granted", Toast.LENGTH_SHORT).show();
return null;
}
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, this);
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return l;
} else {
Toast.makeText(context, "Please enable GPS !", Toast.LENGTH_LONG).show();
}
return null;
}
示例11: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
if (isNetworkEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
示例12: getLastLocation
import android.location.LocationManager; //导入方法依赖的package包/类
private Location getLastLocation() {
LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i = providers.size() - 1; i >= 0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) {
break;
}
}
return l;
}
示例13: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public void getLocation(Fragment fragment) {
this.fragment = fragment;
boolean locationPermissionFlag = util.checkPermission(locationPermissions,
activity);
if (locationPermissionFlag) {
fragment.requestPermissions(locationPermissions, Configure.LOCATION_PERMISSION_CODE);
}else{
locationManager = (LocationManager) activity.getSystemService(Context.
LOCATION_SERVICE);
String provider;
List<String> providerList = locationManager.getProviders(true);
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
Toast.makeText(activity, "请连接网络或打开GPS",
Toast.LENGTH_LONG).show();
return;
}
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 2000, 10, this);
if (location != null) {
getLocation(location);
}
}
}
示例14: getProvider
import android.location.LocationManager; //导入方法依赖的package包/类
public static String getProvider(Context ctx, LocationManager mLocationManager) {
String mProviderName = "";
if (isNetworkConnected(ctx)) {
boolean hasGpsProvide;
boolean hasNetWorkProvider;
if (mLocationManager.getProvider("gps") != null) {
hasGpsProvide = true;
} else {
hasGpsProvide = false;
}
if (mLocationManager.getProvider("network") != null) {
hasNetWorkProvider = true;
} else {
hasNetWorkProvider = false;
}
Helper.showLog(TAG, " hasGpsProvide =" + hasGpsProvide + " hasNetWorkProvider =" +
hasNetWorkProvider);
boolean isGpsEnabled = isGPSProviderAvaliable(ctx);
boolean isWIFIEnabled = isWIFIProviderAvaliable(ctx);
if (!hasGpsProvide && !hasNetWorkProvider) {
Helper.showToast(ctx, (int) R.string.xw);
return ctx.getString(R.string.xw);
} else if (!hasGpsProvide && hasNetWorkProvider) {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>only network provider");
if (isWIFIEnabled) {
mProviderName = "network";
} else {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>no network avaliable");
return null;
}
} else if (!hasGpsProvide || hasNetWorkProvider) {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>hasallprovider");
if (!isGpsEnabled && !isWIFIEnabled) {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>no GPS or NETWORK avaliable");
return null;
} else if (isGpsEnabled && !isWIFIEnabled) {
mProviderName = "gps";
} else if (!isGpsEnabled && isWIFIEnabled) {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>network avaliable");
mProviderName = "network";
} else if (isGpsEnabled && isWIFIEnabled) {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>all avaliable");
mProviderName = "gps";
if (mLocationManager.getLastKnownLocation("gps") == null) {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>all avaliable but location is null");
mProviderName = "network";
}
}
} else {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>only GPS provider");
if (isGpsEnabled) {
mProviderName = "gps";
} else {
Helper.showLog(TAG, ">>>>>>>>>>>>>>>no GPS avaliable");
return null;
}
}
}
Helper.showToast(ctx, ctx.getString(R.string.gu));
return mProviderName;
}
示例15: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
showSettingsAlert();
}
else {
this.canGetLocation = true;
if (isNetworkEnabled) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (mContext.checkSelfPermission(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 overriding
// 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 null;
}
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
mLastestLocation = new LatLng(latitude, longitude);
return location;
}