我正在使用 Google Play 服务来查找当前位置。但它不会出现在任何回调函数中。无法连接 Google API 客户端。
这是我的LocationUtil
课程代码:
package com.steporganization.util;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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;
/**
* Created by Yogesh on 26-07-2015.
*/
public class LocationUtil implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener{
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
private final static String TAG="LocationUtil_Yogesh";
private GoogleApiClient client;
private Location location;
private boolean isClientConnected=false;
private Activity baseActivity;
private Context context;
private boolean isPlayServicesAvailable()
{
int resultCode= GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if (resultCode!= ConnectionResult.SUCCESS)
{
if(GooglePlayServicesUtil.isUserRecoverableError(resultCode))
{
GooglePlayServicesUtil.getErrorDialog(resultCode,baseActivity,PLAY_SERVICES_RESOLUTION_REQUEST).show();
return false;
}
else
{
Toast.makeText(context,
"This device is not supported.", Toast.LENGTH_LONG)
.show();
return false;
}
}
return true;
}
private synchronized void buildGoogleAPiClient()
{
client=new GoogleApiClient.Builder(context).
addConnectionCallbacks(this).addOnConnectionFailedListener(this).
addApi(LocationServices.API).build();
}
private Location getLocation()
{
Location lastLocation;
lastLocation= LocationServices.FusedLocationApi.getLastLocation(client);
if(lastLocation==null)
{
//check for location
//requestlocation update until some location is acquired or timeout of 10 seconds
Long initialTime= System.currentTimeMillis();
Long finalTime=System.currentTimeMillis();
createLocationRequest();
while(this.location==null || finalTime-initialTime<20000)
{
finalTime=System.currentTimeMillis();
}
if(this.location==null)
{
Log.d(TAG,"Unable to fetch location");
LocationServices.FusedLocationApi.removeLocationUpdates(client,this);
return null;
}
else
LocationServices.FusedLocationApi.removeLocationUpdates(client,this);
return this.location;
}
else
return lastLocation;
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG,"onConnected Called");
isClientConnected=true;
location=getLocation();
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG,"onConnection suspended Called");
client.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG,"onConnection Failed Called");
Log.d(TAG, "Error Code: " + connectionResult.getErrorCode());
}
protected void createLocationRequest()
{
LocationRequest locationRequest=new LocationRequest().
setInterval(1000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
this.location=location;
}
public Location getUserCurrentLocation(Activity activity,Context context)
{
this.baseActivity=activity;
this.context=context;
if(isPlayServicesAvailable())
{
Log.d(TAG, "play services available");
buildGoogleAPiClient();
if(client!=null)
{
client.connect();
}
else
{
Log.d(TAG,"Cleint null");
}
while(isClientConnected!=true)
{
Log.d(TAG,"connecting API client");
}
return this.location;
}
else {
Log.d(TAG, "play service not available");
return null;
}
}
}
在另一个活动中,我正在创建此类的一个对象并调用getUserCurrentLocation(this,getApplicationContext())
函数。
谁能建议我在这里做错了什么?此外,它不是正确的代码,但我的观点是为什么它没有到达任何回调函数内部,以便我可以根据我的要求修改我的逻辑。
另外,我发现了一个类似的问题,但还没有回答。