gpt4 book ai didi

android - 等待 onConnected 调用,获取位置

转载 作者:行者123 更新时间:2023-11-29 19:20:13 41 4
gpt4 key购买 nike

因此,我有自定义位置服务类,我想从中获取最后已知的位置。我有可能在连接 GoogleApiClient 之前调用 getLastKnownLocation(),所以我必须等待它然后调用 getLastKnownLocation(),但我不知道如何管理那个。我认为 RxJava 2 可以帮助我解决这个问题,但我还不熟悉该框架。这是我现在的类(class):

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;

import javax.inject.Inject;

import pl.pancor.android.air.base.FragmentScope;

@FragmentScope
public class LocationService implements Location.Service,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
ActivityCompat.OnRequestPermissionsResultCallback {

private static final String TAG = LocationService.class.getSimpleName();
private static final int PERMISSIONS_REQUEST = 13;

private GoogleApiClient mGoogleApiClient;

private Activity mActivity;

private android.location.Location mLastLocation;

private Location.Receiver mReceiver;

@Inject
LocationService(Activity activity) {

mActivity = activity;
}

@Override
public void getLastKnownLocation() {

if (isPermissionsGranted(true))
getLocation();

}

/**
* @param request if permissions aren't granted and {@param request} is true,
* then request permissions
* @return true if location permissions are granted
*/
private boolean isPermissionsGranted(boolean request) {

if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {

if (request) {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST);
}
return false;
}
return true;
}

private void getLocation() {

if (mGoogleApiClient != null)

mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);

if (mLastLocation != null) {

LatLng latLng = new LatLng(mLastLocation.getLatitude(),
mLastLocation.getLongitude());
mReceiver.lastKnownLocation(latLng);
} else {

Log.e(TAG, "NULLLLLLLLLLLLLLLLLLLLLLL");
}
}

@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {


}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void setupReceiver(Location.Receiver receiver) {

mReceiver = receiver;
}

@Override
public void onStart() {

if (mGoogleApiClient != null){

mGoogleApiClient.connect();
} else {

mGoogleApiClient = getGoogleApiClient();
mGoogleApiClient.connect();
}
}

@Override
public void onStop() {

if (mGoogleApiClient != null)
mGoogleApiClient.disconnect();
}

private GoogleApiClient getGoogleApiClient(){

return new GoogleApiClient.Builder(mActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {

switch (requestCode){

case PERMISSIONS_REQUEST:

if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED){

getLastKnownLocation();
} else {

}
}
}
}

我需要以某种方式检查 mGoogleApiClient 是否已连接 (mGoogleApiClient.isConnected()),如果未连接,请等待连接,然后从 FusedLocationApi 获取位置,但我不想放置方法到 onConnected(),因为它有时会在我不想返回位置时返回位置。

最佳答案

在你在评论区解释之后,我会做这样的事情:

  1. 在 Receiver/Fragment 类中,当 onConnected 适合调用 mReceiver.lastKnownLocation(latLng)< 时,我会在 LocationService 类中放置一些将变量“updateUI”设置为 true 的逻辑方法。默认值为 false,如果在接收器准备好之前调用 onConnected,方法 mReceiver.lastKnownLocation(latLng)不会被调用。

  2. 另一种方法是在您的 SharedPreferences 中(或至少在 onPause 方法中)始终存储最后已知的位置。然后,您始终可以在第一次需要位置时使用它,并等待稍后更精确的位置,但这种方法在开始时不会那么精确。

关于android - 等待 onConnected 调用,获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42675004/

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