问题描述
我使用GoogleApiClient获取用户位置。我创建了一个单例类来获取用户位置。这里是这个类:
public class LocationUtils {
private static LocationUtils locationUtils;
私有GoogleApiClient googleApiClient;
public static LocationUtils getInstance(){
if(locationUtils == null)
locationUtils = new LocationUtils();
返回locationUtils;
private LocationUtils(){}
public Location getLocation(Context context){
final Location [] location = {空值};
googleApiClient = new GoogleApiClient.Builder(App.getContext())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks(){
@Override
public void onConnected(Bundle Bundle){
if(ContextCompat.checkSelfPermission(App.getContext(),Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)
location [0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
googleApiClient.disconnect();
}
@Override
public void onConnectionSuspended(int i){
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener(){
@Override
public void onConnectionF ailed(ConnectionResult connectionResult){
}
})
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
return location [0];
}
}
我想通过LocationUtils.getInstance()。getLocation(context)获取用户位置。当我调用getLocation()方法时,它应该连接到GoogleApiClient,返回用户位置并断开连接到每个调用的GoogleApiClient。但是当我调用该方法时,它将返回null。主线程不会等待onConnected()方法,并将位置对象返回为null。它如何等待onConnect()方法?或者我怎么能在onConnected()中返回Location对象?
已解决根据Tim Castelijns的回答
首先我创建了一个接口:
public interface LocationCallbackInterface {
void onComplete(Location location);
}
然后在getLocationMethod()中使用它:
public void getLocation(Context context,final LocationCallbackInterface callback){
final Location [] location = {空值};
googleApiClient = new GoogleApiClient.Builder(App.getContext())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks(){
@Override
public void onConnected(Bundle Bundle){
if(ContextCompat.checkSelfPermission(App.getContext(),Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
location [0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
//Toast.makeText(App.getContext(),location came:+ location [0] .toString(),Toast.LENGTH_LONG).show();
callback.onComplete(location [ 0]);
}
googleApiClient.disconnect();
}
@Override
public void onConnectionSuspended(int i){
}
})
.addOnConnectionFailedListener(新的GoogleApiClient.OnConnect ctionFailedListener(){
@Override
public void onConnectionFailed(ConnectionResult connectionResult){
$ b})
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
然后调用该方法一个活动:
$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b @Override
public void onComplete(Location location){
if(location!= null){
Toast.makeText(getActivity(),location:+ location.toString() ,Toast.LENGTH_SHORT).show();
initCamera(location);
} else
Toast.makeText(getActivity(),location is null,Toast.LENGTH_SHORT).show( );
}
});
由于位置是异步获取的,因此您应该使用回调返回的位置,否则您的返回值将始终为空,因为googleApiClient尚未完成。
定义一个类似这样的接口
public interface LocationCallback {
void onComplete(Location location);
更改
public Location getLocation(Context context)
to
public Location getLocation(Context context,LocationCallback callback)
并且像这样称呼它
LocationUtils.getLocation(context,new LocationCallback(){
@Override
onComplete(位置位置){
//当调用这个函数时,您将获得一个位置
}
});
I am using GoogleApiClient to get user location. I created a singleton class to get user location. Here is that class:
public class LocationUtils {
private static LocationUtils locationUtils;
private GoogleApiClient googleApiClient;
public static LocationUtils getInstance() {
if(locationUtils == null)
locationUtils = new LocationUtils();
return locationUtils;
}
private LocationUtils() {}
public Location getLocation(Context context){
final Location[] location = {null};
googleApiClient = new GoogleApiClient.Builder(App.getContext())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(App.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
location[0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient );
googleApiClient.disconnect();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.addApi( LocationServices.API )
.build();
googleApiClient.connect();
return location[0];
}
}
I want to get user location with LocationUtils.getInstance().getLocation(context) . When i call getLocation() method, it should connect to GoogleApiClient, return user location and disconnect to GoogleApiClient for every call. But when i call that method, it returns null. The main thread does not wait for onConnected() method and returns location object as null. How can it waits for onConnect() method ? OR how can i return Location object in onConnected() ?
SOLVED according to Tim Castelijns's answer
First i created an interface:
public interface LocationCallbackInterface {
void onComplete(Location location);
}
then used it in getLocationMethod() :
public void getLocation(Context context, final LocationCallbackInterface callback){
final Location[] location = {null};
googleApiClient = new GoogleApiClient.Builder(App.getContext())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(App.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
location[0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
//Toast.makeText(App.getContext(), "location came :" + location[0].toString(), Toast.LENGTH_LONG).show();
callback.onComplete(location[0]);
}
googleApiClient.disconnect();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.addApi( LocationServices.API )
.build();
googleApiClient.connect();
}
then called that method in an activity:
LocationUtils.getInstance().getLocation(getActivity(), new LocationCallbackInterface() {
@Override
public void onComplete(Location location) {
if (location != null) {
Toast.makeText(getActivity(), "location :" + location.toString(), Toast.LENGTH_SHORT).show();
initCamera(location);
} else
Toast.makeText(getActivity(), "location is null", Toast.LENGTH_SHORT).show();
}
});
Since the location is acquired asynchronously, you should use a callback to return the location, otherwise your return value will always be null because the googleApiClient is not done yet.
Define an interface like this
public interface LocationCallback {
void onComplete(Location location);
}
Change
public Location getLocation(Context context)
to
public Location getLocation(Context context, LocationCallback callback)
and call it like this
LocationUtils.getLocation(context, new LocationCallback() {
@Override
onComplete(Location location) {
// when this is called, you will have a location.
}
});
这篇关于如何在GoogleApiClient onConnected()上返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!