gpt4 book ai didi

java - 如何从非 Activity 类中删除 FusedlocationProviderClient 位置更新

转载 作者:行者123 更新时间:2023-11-29 00:02:45 25 4
gpt4 key购买 nike

我正在尝试编写一个实用程序类来包装 Google Play 服务 FusedLocationProviderClient API 和位置权限请求,因为每次我想向应用程序添加位置功能时,我都厌倦了编写所有这些样板文件。我遇到的问题是,一旦我开始位置更新,我就无法删除它们。这是我的实用程序类的相关部分:

public class UserLocationUtility extends LocationCallback
{
// Hold a WeakReference to the host activity (allows it to be garbage-collected to prevent possible memory leak)
private final WeakReference<Activity> weakActivity;
// Debug tag
private static final String TAG = "UserLocationUtility";


public static class RequestCodes
{
static final int CURRENT_LOCATION_ONE_TIME = 0;
static final int CURRENT_LOCATION_UPDATES = 1;
static final int LAST_KNOWN_LOCATION = 2;
static final int SMART_LOCATION = 3;
}


private FusedLocationProviderClient mLocationClient;
private Context mContext;
private LocationRequest mLocationRequest;


/* Constructor */
UserLocationUtility(Activity activity){
// assign the activity to the weak reference
this.weakActivity = new WeakReference<>(activity);

// Hold a reference to the Application Context
this.mContext = activity.getApplicationContext();

// Instantiate our location client
this.mLocationClient = LocationServices.getFusedLocationProviderClient(mContext);

// Set up the default LocationRequest parameters
this.mLocationRequest = new LocationRequest();
setLocationRequestParams(2000, 500, LocationRequest.PRIORITY_HIGH_ACCURACY);
// Sets up the LocationRequest with an update interval of 30 seconds, a fastest
// update interval cap of 5 seconds and using balanced power accuracy priority.
} /* Note: values for testing only. Will be dialed back for better power management when testing complete */


/* Stripped out other methods for brevity */


@SuppressLint("MissingPermission")
public void getCurrentLocationOneTime(final UserLocationCallback callback){

mLocationClient.requestLocationUpdates(mLocationRequest, new LocationCallback()
{
@Override
public void onLocationResult(LocationResult locationResult){
if (locationResult == null){
callback.onFailedRequest("getCurrentLocationOneTime(): Request failed: returned null");
return;
}

callback.onLocationResult(locationResult.getLastLocation());
stopLocationUpdates(); /* Stopping location updates here just for testing (NOT WORKING!!) */

}
}, null);

}


public void stopLocationUpdates(){

mLocationClient.removeLocationUpdates(new LocationCallback(){});
Log.i(TAG, "stopLocationUpdates(): Location updates removed");

}

}

这是我尝试使用它的方式(来自 MainActivity):

UserLocationUtility locationUtility;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


locationUtility = new UserLocationUtility(this);

if (locationUtility.checkPermissionGranted()){
Log.i(TAG, "Permissions are granted.");
getLocationUpdates();
} else {
Log.i(TAG, "Permissions are not granted. Attempting to request...");
locationUtility.requestPermissions(UserLocationUtility.RequestCodes.CURRENT_LOCATION_UPDATES);
}


}

public void getLocationUpdates(){

locationUtility.getCurrentLocationOneTime(new UserLocationCallback() {
@Override
public void onLocationResult(Location location) {
Log.i(TAG, "getLocationUpdates result: " + location.toString());
}

@Override
public void onFailedRequest(String result) {
Log.e(TAG, "LocationUpdates result: " + result);
}
});

}

这是日志中的示例:

I/MainActivity: getLocationUpdates result: Location[fused 34.421998,-125.084000 hAcc=731 et=+2h10m52s694ms vAcc=??? sAcc=??? bAcc=???]
I/UserLocationUtility: stopLocationUpdates(): Location updates removed
I/MainActivity: getLocationUpdates result: Location[fused 34.421998,-125.084000 hAcc=739 et=+2h10m57s697ms vAcc=??? sAcc=??? bAcc=???]
I/UserLocationUtility: stopLocationUpdates(): Location updates removed
I/MainActivity: getLocationUpdates result: Location[fused 34.421998,-125.084000 hAcc=763 et=+2h11m5s723ms vAcc=??? sAcc=??? bAcc=???]
I/UserLocationUtility: stopLocationUpdates(): Location updates removed
etc...

如您所见,我正在正确接收位置更新,但对 stopLocationUpdates() 的调用不起作用。我感觉这与我将新的 LocationCallback 传递给 removeUpdates() 方法这一事实有关,但我不确定替代方案是什么,或者即使有替代方案。这是一个非 Activity 类,我无法将 LocationCallback 完全初始化为 onCreate() 中的成员,然后根据需要传递它。关于这个的谷歌文档根本没有太大帮助。那是因为我缺乏必要的理解来破译它们,还是因为它们不是很好我不知道,但无论哪种方式我都被难住了并且四处搜索了很多我似乎无法在其他地方找到现有的答案。谢谢。

最佳答案

将我的解决方案作为答案发布,以防对其他人有帮助。

我通过将 LocationCallback 声明为成员变量然后在需要它的每个方法中初始化(或重新初始化)它来使其工作...

public void getCurrentLocationUpdates(final UserLocationCallback callback){
if (mIsReceivingUpdates){
callback.onFailedRequest("Device is already receiving updates");
return;
}

// Set up the LocationCallback for the request
mLocationCallback = new LocationCallback()
{
@Override
public void onLocationResult(LocationResult locationResult){
if (locationResult != null){
callback.onLocationResult(locationResult.getLastLocation());
} else {
callback.onFailedRequest("Location request returned null");
}
}
};

// Start the request
mLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
// Update the request state flag
mIsReceivingUpdates = true;
}

我在方法开始时检查是否已经接收到位置更新,如果接收到则尽早退出。这可以防止重复(因此无法阻止)位置更新请求被发起。

调用 stopLocationUpdates(下面供引用)方法现在可以正常工作。

public void stopLocationUpdates(){

mLocationClient.removeLocationUpdates(mLocationCallback);
mIsReceivingUpdates = false;
Log.i(TAG, "Location updates removed");

}

关于java - 如何从非 Activity 类中删除 FusedlocationProviderClient 位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51733587/

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