android - 当应用程序在 Android 上后台运行时如何更新地图 UI 上的位置
问题描述
当应用程序在 Android 的后台运行时,我想更新地图 UI 上的位置。我知道这可以使用服务类。我试过使用服务类,但我无法实时看到我的用户界面。
private void startLocationUpdates() {
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@SuppressLint("MissingPermission")
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
Log.i(TAG, "All location settings are satisfied.");
Toast.makeText(getApplicationContext(), "Started location updates!", Toast.LENGTH_SHORT).show();
//noinspection MissingPermission
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper());
// Instantiating the class PolylineOptions to plot polyline in the map
PolylineOptions polylineOptions = new PolylineOptions();
// Setting the color of the polyline
polylineOptions.color(Color.RED);
polylineOptions.clickable(true);
// Setting the width of the polyline
polylineOptions.width(3);
// Setting points of polyline
polylineOptions.addAll(points);
// Adding the polyline to the map
mMap.addPolyline(polylineOptions);
updateLocationUI();
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Attempting to upgrade " +
"location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult(MapsActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sie) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
String errorMessage = "Location settings are inadequate, and cannot be " +
"fixed here. Fix in Settings.";
Log.e(TAG, errorMessage);
Toast.makeText(MapsActivity.this, errorMessage, Toast.LENGTH_LONG).show();
}
updateLocationUI();
}
});
}
解决方案
推荐阅读
- python - 将文件选择器获得的文件路径传递给变量并用作 cv2.imread 的输入,但它似乎没有传递任何东西
- python - H2O 中的 GLRM - 使用逆变换检索 X 矩阵
- javascript - Reactjs 和 Javascript:如何将嵌套对象映射到输入字段
- python - 警告:tensorflow:输入图不使用 tf.data.Dataset 或包含 QueueRunner。这意味着永远预测收益率。这可能是一个错误
- spring - 突然,我的项目中所有 Spring Boot Framework 依赖项都丢失了
- arduino-uno - 通过 arduino 与 android 通信
- python - 如何将列表文本添加到 matplotlib 极坐标图
- awk - “=”处的 awk 语法错误,因为变量名在字符串中?
- html - 如何从 HTML 元素中创建一个 newArray?
- r - 如何为整个数据框创建一个虚拟变量?