本文整理汇总了Java中android.location.LocationManager.getProviders方法的典型用法代码示例。如果您正苦于以下问题:Java LocationManager.getProviders方法的具体用法?Java LocationManager.getProviders怎么用?Java LocationManager.getProviders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.location.LocationManager
的用法示例。
在下文中一共展示了LocationManager.getProviders方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLastKnownLocation
import android.location.LocationManager; //导入方法依赖的package包/类
private Location getLastKnownLocation() {
mLocationManager = (LocationManager)InstrumentationRegistry.getTargetContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location loc;
try{
loc = mLocationManager.getLastKnownLocation(provider);
} catch (SecurityException e) {
loc = null;
}
if (loc == null) {
continue;
}
if (bestLocation == null || loc.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = loc;
}
}
return bestLocation;
}
示例2: getLastKnownLocation
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* Gets last known location
* Use only when current location is not available
* @param context Context is required to acquire LocationManager system service
* @return Returns last known location as android.location.Location
*/
public static Location getLastKnownLocation(Context context) {
LocationManager mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
if (providers == null || providers.size() == 0) return null;
try {
for (String provider : providers) {
if (provider.equals(LocationManager.PASSIVE_PROVIDER)) continue;
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) continue;
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) bestLocation = l;
}
} catch (SecurityException ignored) {}
return bestLocation;
}
示例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: getLastKnownLocation
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* Get last known location as in http://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null
* @return the last location known to the device
*/
private Location getLastKnownLocation() {
LocationManager mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
示例5: 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);
}
}
}
示例6: 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;
}
示例7: initLocation
import android.location.LocationManager; //导入方法依赖的package包/类
private void initLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
String locationProvider;
/**
* 如果首选GPS定位,会存在这种情况,上次GPS启动采集数据在A地,本次在B地需要定位,但用户恰好在室内无
* GPS信号,只好使用上次定位数据,就出现了地区级偏差。而网络定位则更具有实时性,在精度要求不高以及室内
* 使用场景更多的前提下,首选网络定位
*/
if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
locationProvider = LocationManager.NETWORK_PROVIDER; // 首选网络定位
} else if (providers.contains(LocationManager.GPS_PROVIDER)) {
locationProvider = LocationManager.GPS_PROVIDER;
} else {
locationProvider = LocationManager.PASSIVE_PROVIDER;
}
if (mLocationListener != null)
mLocationManager.requestLocationUpdates(locationProvider, 2000, 10, mLocationListener);
}
示例8: forceLocationFromCachedProviders
import android.location.LocationManager; //导入方法依赖的package包/类
private static void forceLocationFromCachedProviders(LocationManager locationManager)
{
List<String> providers = locationManager.getProviders(true);
Location bestCachedLocation = null;
for (String provider : providers)
{
Location l = null;
try {
l = locationManager.getLastKnownLocation(provider);
}
catch (SecurityException e)
{
continue;
}
if (l == null)
{
continue;
}
if (isBetterLocation(l, bestCachedLocation))
{
bestCachedLocation = l;
}
}
LocationService.bestLocation = bestCachedLocation;
Log.v("Location", "best location set from cache");
}
示例9: isGpsEnabled
import android.location.LocationManager; //导入方法依赖的package包/类
/**
* 检测GPS是否打开
*
* @param context 上下文
* @return 结果
*/
public static boolean isGpsEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> accessibleProviders = lm.getProviders(true);
for (String name : accessibleProviders) {
if ("gps".equals(name)) {
return true;
}
}
return false;
}
示例10: fetchLocationUpdates
import android.location.LocationManager; //导入方法依赖的package包/类
@SuppressLint("MissingPermission")
static void fetchLocationUpdates(Context appContext) {
if (appContext != null) {
Location lastLocation = null;
// Does app developer allow us to use location?
if (locationEnabled) {
LogUtil.d(TAG, "Updating location.");
// Save last location
if (location != null) {
lastLocation = location;
}
// fetch latest location info from location provider
if (appContext != null
&& (appContext.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION") == PackageManager.PERMISSION_GRANTED
|| appContext.checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION") == PackageManager.PERMISSION_GRANTED)) {
// Get lat, long from any GPS information that might be currently
// available
LocationManager lm = (LocationManager) appContext
.getSystemService(Context.LOCATION_SERVICE);
for (String provider_name : lm.getProviders(true)) {
LogUtil.v(TAG, "Location provider_name::" + provider_name);
Location l = lm.getLastKnownLocation(provider_name);
if (l == null) {
continue;
}
if (lastLocation == null) {
lastLocation = l;
} else {
if (l.getTime() > 0 && lastLocation.getTime() > 0) {
if (l.getTime() > lastLocation.getTime()) {
lastLocation = l;
}
}
}
}
} else {
LogUtil.d(TAG,
"Location permissions ACCESS_COARSE_LOCATION and/or ACCESS_FINE_LOCATION aren\\'t set in the host app. Unable to update location data.");
}
}
// Set the location info back to the application
// This will set the saved location to null if location was not enabled
if (location != lastLocation) {
location = lastLocation;
}
}
}