我使用Fused位置提供程序开发了一个应用程序。在onConnected()
方法中,我正在请求位置更新,并且将启动应用程序逻辑并调用onLocationChanged()
。
问题:在美国的设备中未调用onLocationChanged()
方法。此代码在INDIA中的设备上运行良好,但在美国不起作用。如果不起作用,我的意思是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");
}
}
任何人都可以帮我解决这个问题吗?我在这里缺少什么吗?
答案 0 :(得分:0)
也许是因为手机无法与服务提供商建立联系?
尝试使用gps。
String locationProvider = LocationManager.GPS_PROVIDER;
// Or, use GPS location data:
// String locationProvider = LocationManager.NETWORK_PROVIDER;
答案 1 :(得分:0)
我认为您正在使用NETWORK_PROVIDER来访问/获取位置更新。
尝试使用 GPS_PROVIDER 或使用 PASSIVE_PROVIDER 从NETWORK_PROVIDER或GPS_PROVIDER获取位置。
确保您在清单中拥有访问NETWORK和GPS位置的权限。
答案 2 :(得分:0)
如果您使用FusedLocationProviderApi
,则可以选择使用SettingsApi
检查设备是否具有应用程序所需的位置设置。 SettingsApi
并可选择提供位置对话框,以便在发现设备不足时更新设备的位置设置。你可以看一下example。在Settings
中关闭位置运行示例,您应该看到对话框。您的应用可能会失败,因为它没有足够的权限,位置对话框可能会有所帮助。