本文整理汇总了Java中android.location.LocationManager.getBestProvider方法的典型用法代码示例。如果您正苦于以下问题:Java LocationManager.getBestProvider方法的具体用法?Java LocationManager.getBestProvider怎么用?Java LocationManager.getBestProvider使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.location.LocationManager
的用法示例。
在下文中一共展示了LocationManager.getBestProvider方法的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: 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;
}
示例3: 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;
}
示例4: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public static String getLocation(Context context) {
if (context == null) {
return "";
}
try {
LocationManager locationManager = (LocationManager) context.getSystemService(HOME_RECOMMEND_PARAMETERS.LOCATION);
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);
criteria.setAccuracy(2);
String bestProvider = locationManager.getBestProvider(criteria, true);
if (bestProvider != null) {
Location lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);
if (lastKnownLocation == null) {
return "";
}
double latitude = lastKnownLocation.getLatitude();
g = latitude + "*" + lastKnownLocation.getLongitude();
return g;
}
} catch (Throwable e) {
f.b("getLocation", "getLocation>>>", e);
}
return "";
}
示例5: 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(Context context,long minTime, long minDistance, OnLocationChangeListener listener) {
if (listener == null) return false;
mLocationManager = (LocationManager) Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
mListener = listener;
if (!isLocationEnabled()) {
return false;
}
String provider = mLocationManager.getBestProvider(getCriteria(), true);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return false;
}
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;
}
示例6: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public static String getLocation(Context context) {
if (context == null) {
return "";
}
try {
LocationManager locationManager = (LocationManager) context.getSystemService
("location");
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);
criteria.setAccuracy(2);
String bestProvider = locationManager.getBestProvider(criteria, true);
if (bestProvider != null) {
Location lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);
if (lastKnownLocation == null) {
return "";
}
double latitude = lastKnownLocation.getLatitude();
g = latitude + "*" + lastKnownLocation.getLongitude();
return g;
}
} catch (Throwable e) {
f.b("getLocation", "getLocation>>>", e);
}
return "";
}
示例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(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;
}
示例8: onCreate
import android.location.LocationManager; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
localM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myProvider = localM.getBestProvider(new Criteria(), false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
myLocation = localM.getLastKnownLocation(myProvider);
if (myLocation != null) {
Log.i("WJH", myLocation.toString());
} else {
Log.i("WJH", "Location is null.");
}
}
示例9: doInBackground
import android.location.LocationManager; //导入方法依赖的package包/类
@Override
protected Integer doInBackground(Context... params) {
HttpResponse response;
try {
LocationManager lm = (LocationManager) params[0].getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(Utils.WEATHER_URL + "lat=" +
loc.getLatitude() +
"&lon=" +
+loc.getLongitude()
+ "&units=metric"));
response = client.execute(request);
String result = EntityUtils.toString(response.getEntity());
JSONObject jsonResponse = new JSONObject(result);
JSONObject jsonWeather = jsonResponse.getJSONObject("main");
return jsonWeather.getInt("temp");
} catch (Exception e) {
error = e;
}
return 0;
}
示例10: getLastKnownLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public static Location getLastKnownLocation(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
final String providerName = locationManager.getBestProvider(criteria, true);
if (providerName == null)
return null;
if (PermissionHelper.checkCoarseLocationPermission(context)) {
return locationManager.getLastKnownLocation(providerName);
} else {
return null;
}
}
示例11: getLocation
import android.location.LocationManager; //导入方法依赖的package包/类
public Location getLocation(Activity activity)
{
LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, false); // 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
return location;
}
示例12: isLocationEnable
import android.location.LocationManager; //导入方法依赖的package包/类
public static boolean isLocationEnable(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria cri = new Criteria();
cri.setAccuracy(Criteria.ACCURACY_COARSE);
cri.setAltitudeRequired(false);
cri.setBearingRequired(false);
cri.setCostAllowed(false);
String bestProvider = locationManager.getBestProvider(cri, true);
return !TextUtils.isEmpty(bestProvider);
}
示例13: selectCurrentLocation
import android.location.LocationManager; //导入方法依赖的package包/类
protected void selectCurrentLocation(boolean forceUseGps) {
setCurrentLocation = true;
selectedLocationId = 0;
if (transaction.isTemplateLike()) {
if (isShowLocation) {
locationText.setText(R.string.current_location);
}
return;
}
// Start listener to find current location
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);
if (provider != null) {
lastFix = locationManager.getLastKnownLocation(provider);
}
if (lastFix != null) {
setLocation(lastFix);
connectGps(forceUseGps);
} else {
// No enabled providers found, so disable option
if (isShowLocation) {
locationText.setText(R.string.no_fix);
}
}
}
示例14: onCreate
import android.location.LocationManager; //导入方法依赖的package包/类
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.activity_maps);
localdb = openOrCreateDatabase("localdb",MODE_PRIVATE,null);
localdb.execSQL("CREATE TABLE IF NOT EXISTS position(imei NVARCHAR(100), latitude NVARCHAR(100),longitude NVARCHAR(100), timestamp DATETIME, out NVARCHAR(1));");
localdb.execSQL("DELETE FROM position WHERE timestamp <= date('now','-2 day');"); // to keep data in last day on user phone
setUpMapIfNeeded();
this.mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
this.mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 0, this);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String myImei = tm.getDeviceId();
timer = new Timer();
timer.schedule(new RunMarker(this.mMap, myImei,this), 0, 20000);
editTextFromDate = (EditText) findViewById(R.id.editText);
search = (Button) findViewById(R.id.button);
DateField fromDate = new DateField(editTextFromDate,search, this, mMap, timer);
}
示例15: onCreate
import android.location.LocationManager; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myProvider = localM.getBestProvider(new Criteria(), false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
myLocation = localM.getLastKnownLocation(myProvider);
if (myLocation != null) {
Log.i("WJH", myLocation.toString());
} else {
Log.i("WJH", "Location is null.");
}
}