0

我使用 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");
    }
}

谁能帮我解决这个问题?我在这里缺少什么吗?

4

3 回答 3

0

如果您正在使用FusedLocationProviderApi,您可以选择使用SettingsApi来检查设备是否具有应用程序所需的位置设置。如果发现设备的SettingsApi位置设置不合适,则可以选择提供位置对话框来更新设备的位置设置。你可以看一个例子。在关闭位置的情况下运行示例Settings,您应该会看到对话框。您的应用程序可能会失败,因为它没有足够的权限,而位置对话框可能会有所帮助。

于 2015-10-06T15:59:09.897 回答
0

我认为您正在使用 NETWORK_PROVIDER 访问/获取位置更新。

尝试使用GPS_PROVIDER或使用PASSIVE_PROVIDER从 NETWORK_PROVIDER 或 GPS_PROVIDER 获取位置。

确保您在清单中具有访问 NETWORK 和 GPS 位置的权限。

于 2015-09-04T12:41:37.167 回答
0

也许是电话无法与服务提供商连接的事实?

尝试使用gps。

String locationProvider = LocationManager.GPS_PROVIDER;
// Or, use GPS location data:
// String locationProvider = LocationManager.NETWORK_PROVIDER;
于 2015-09-04T12:22:33.660 回答