我使用 Fused 位置提供程序开发了一个应用程序。在该onConnected()
方法中,我请求位置更新,并且将启动并onLocationChanged()
调用应用程序逻辑。
问题:onLocationChanged()
在美国的设备中没有调用方法。此代码在印度的设备上运行良好,但在美国无法运行。不起作用,我的意思是locationClient
连接但从onLocationChanged()
未被调用。
下面的代码:
public class LocationReceiver extends BroadcastReceiver
implements
// GooglePlayServicesClient.ConnectionCallbacks,
// GooglePlayServicesClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
// LocationClient locationclient = null;
GoogleApiClient locationclient = null;
Context contxt;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
@Override
public void onReceive(Context context, Intent intent) {
contxt = context;
// Log.i("locationreciever", "in location rec");,
Log.i("fused", "in location rec");
int resp = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
if (resp == ConnectionResult.SUCCESS) {
// locationclient = new LocationClient(context, this, this);
locationclient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)// (mConnectionCallbacks)
.addOnConnectionFailedListener(this)// (mOnConnectionFailedListener)
.build();
locationclient.connect();
} else {
Log.i("fused", "loc client Google Play Service Error");
}
}
public void updateTransientLocation(Context context, Location loc) {
// Log.i("updateTransientLocation", "in fn");
float lat = (float) loc.getLatitude();
float lon = (float) loc.getLongitude();
float acc = loc.getAccuracy();
float alt = (float) loc.getAltitude();
if (lat > 0 && lon > 0) {
PreferenceForApp prefs = new PreferenceForApp(contxt);
prefs.setTransientLatitude(lat);
prefs.setTransientLongitude(lon);
prefs.setTransientAccuracy(acc);
prefs.setTransientAltitude(alt);
}
}
@Override
public void onLocationChanged(Location location) {
Log.i("fused",
" onLocationChanged Location Request :"
+ location.getLatitude() + ","
+ location.getLongitude() + " acc "
+ location.getAccuracy()+" alt "+location.getAltitude());
//TODO wait for some time to get location
updateTransientLocation(contxt, location);
if (locationclient != null) {
if (locationclient.isConnected()) {
// locationclient.removeLocationUpdates(this);
LocationServices.FusedLocationApi.removeLocationUpdates(
locationclient, this);
locationclient.disconnect();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
PreferenceForApp prefs = new PreferenceForApp(contxt);
// if (arg0.hasResolution()) {
// try {
// // Start an Activity that tries to resolve the error
// arg0.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
// } catch (IntentSender.SendIntentException e) {
// e.printStackTrace();
// }}else{
Log.i("fused", "loc client connection failed");
prefs.setGooglePlayServiceErrorCode(arg0.getErrorCode());
}
//}
@Override
public void onConnected(Bundle arg0) {
PreferenceForApp prefs = new PreferenceForApp(contxt);
prefs.setGooglePlayServiceErrorCode(0);
Log.i("fused", "loc client onConnected");
LocationRequest locationrequest = new LocationRequest();
locationrequest
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// PRIORITY_BALANCED_POWER_ACCURACY
// locationclient.requestLocationUpdates(locationrequest, this);
LocationServices.FusedLocationApi.requestLocationUpdates(
locationclient, locationrequest, this); // mLocationListener);
}
// @Override
// public void onDisconnected() {
// Log.i("fused", "loc client disconnected");
// }
@Override
public void onConnectionSuspended(int arg0) {
Log.i("fused", "loc client onConnectionSuspended");
}
}
谁能帮我解决这个问题?我在这里缺少什么吗?