gpt4 book ai didi

android - 如何使用位置管理器但没有位置监听器获取 Android 当前位置 "only on demand"?

转载 作者:行者123 更新时间:2023-11-29 01:22:38 24 4
gpt4 key购买 nike

我的应用不需要实时位置更新。它仅在(1)应用程序启动时需要位置; (2)打开一个特定的 Activity/fragment ; (3)用户点击“获取我的位置”按钮。

标准是:当需要时,定位必须很快,即使它是粗略的(如果可能)最小电池消耗。我计划使用以下方法:

public void getLocation() {
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

if(location != null) {
//save the location to shared prefs to use when needed
return;
}

location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if(location != null) {
//save the location to shared prefs to use when needed
return;
}

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(location != null) {
//save the location to shared prefs to use when needed
return;
}

// if reached that point the location cannot be got!!
}

计划是在每次应用程序启动、激活特定页面或用户想要获取当前位置时调用该方法。除非这些操作触发位置更新,否则旧位置或无位置就足够了。这种方法有意义吗?我可以在不实现 Location Listener 和/或调用方法“requestLocationUpdates()”的情况下这样做吗?我担心的是“getLastKnownLocation()”在调用时可能不会提供当前位置,因为我不确定提供商的“最后已知位置”更新的频率或条件。

非常感谢任何建议和评论。

最佳答案

使用以下代码通过 Google Location Services API 获取当前和最后已知位置

//Global Variable
Location location;
public class MainActivity extends ActionBarActivity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
LocationListener {

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds

@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}

@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}

@Override
public void onConnected(Bundle bundle) {
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location);
}
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}

AnyActivity.class

MainActivity main = new MainActivity();
Location location = main.location;

关于android - 如何使用位置管理器但没有位置监听器获取 Android 当前位置 "only on demand"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com