【问题标题】:OnLocationChanged() ExceptionOnLocationChanged() 异常
【发布时间】:2015-09-04 11:35:59
【问题描述】:

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

谁能帮我解决这个问题?我在这里有什么遗漏吗?

【问题讨论】:

    标签: android gps google-play-services location-services fusedlocationproviderapi


    【解决方案1】:

    可能是手机连接不上服务商?

    尝试使用 GPS。

    String locationProvider = LocationManager.GPS_PROVIDER;
    // Or, use GPS location data:
    // String locationProvider = LocationManager.NETWORK_PROVIDER;
    

    【讨论】:

    • 如何在 FusedLocationApi 中使用位置管理器,?通过使用 FusedLocation,我已经通过 GPS 更新了位置
    【解决方案2】:

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

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

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

    【讨论】:

    • 但我使用的是 FusedLocationApi,它适用于所有(GPS_provider,Network_provider)。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多