gpt4 book ai didi

java - 如何在可以运行直到用户停止的服务中创建 FusedLocationProviderClient?

转载 作者:行者123 更新时间:2023-12-01 14:10:55 27 4
gpt4 key购买 nike

我已经成功创建了一个带有 的 Activity FusedLocationProviderClient 返回特定时间段内的位置坐标。

但我希望它在后台服务中运行,该服务将坐标保存在本地数据库中,并且在用户手动停止服务之前永远不会结束。

这是我的源代码:

private String mLastUpdateTime;
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 5000;
private static final int REQUEST_CHECK_SETTINGS = 100;
private FusedLocationProviderClient mFusedLocationClient;
private SettingsClient mSettingsClient;
private LocationRequest mLocationRequest;
private LocationSettingsRequest mLocationSettingsRequest;
private LocationCallback mLocationCallback;
private Location mCurrentLocation;

private void init() {
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
mSettingsClient = LocationServices.getSettingsClient(this);

mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
// location is received
mCurrentLocation = locationResult.getLastLocation();
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());

updateUIAndMarker();
}
};

mRequestingLocationUpdates = false;

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
}

最佳答案

我会看一下 Google 的 LocationUpdatesForegroundService 示例项目:

https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService

看起来@Al-Amin 发布的解决方案来自同一个地方。尽管在没有署名的情况下发布其他人编写的代码一点也不酷。

关于java - 如何在可以运行直到用户停止的服务中创建 FusedLocationProviderClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53257995/

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