Android locationClient не подключено, исключение выдается изнутри OnConnected
У меня есть приложение для Android, которое использует геозоны, добавляются в следующем коде:
public void register(ArrayList<Geofence> geofences){
if(geofences == null || geofences.size() == 0) return;
this.geofences = geofences;
locationClient = new LocationClient(context, this, onConnectionFailedListener);
locationClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
locationClient.addGeofences(geofences, GeofenceBroadcastReceiver.getPendingIntent(context), this);
}
@Override
public void onDisconnected() {
this.locationClient = null;
}
@Override
public void onAddGeofencesResult(int statusCode, String[] strings) {
if(statusCode != LocationStatusCodes.SUCCESS){
Log.e(getClass().getName(), "onAddGeofencesResult: " + statusCode);
return;
}
listener.addRegistered(strings);
locationClient.disconnect();
}
Время от времени я получаю следующее исключение (ссылка Crashlytics: http://crashes.to/s/b63ac8d5f19):
java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.ff.bT()
at com.google.android.gms.internal.hc.addGeofences()
at com.google.android.gms.location.LocationClient.addGeofences()
at nl.auxilium.autoreset.GeofenceManager$AddGeofenceManager.onConnected(GeofenceManager.java:140)
at com.google.android.gms.internal.ff$c.onConnected()
at com.google.android.gms.internal.fg.b()
at com.google.android.gms.internal.fg.bV()
at com.google.android.gms.internal.ff$h.b()
at com.google.android.gms.internal.ff$h.a()
at com.google.android.gms.internal.ff$b.eN()
at com.google.android.gms.internal.ff$a.handleMessage()
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(NativeStart.java)
Учитывая Исключение, locationClient не подключен, но код используется как метод onConnected.
Может ли кто-нибудь указать мне правильное направление, я не могу воспроизвести эту ошибку на моей разработке Galaxy S4.
2 ответа
Ниже описано, как я исправил эту проблему. Я просто перехватываю IllegalStateException, а затем разрабатываю события, как если бы ни один поставщик местоположения не присутствовал (ShowDefaultLocationData() - это метод, который заботится о вещах, если GPS недоступен).
private void requestLocation() {
if (servicesConnected()) {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
updateLocation = true;
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(1000);
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
}
} else
{
ShowDefaultLocationData();
}
}
@Override
public void onConnected(Bundle connectionHint) {
try {
mLocationClient.requestLocationUpdates(mLocationRequest, MainActivity.this);
isconnected = true;
} catch (IllegalStateException e) {
ShowDefaultLocationData();
}
}
Вы должны установить запрос на подключение
locationClient.requestLocationUpdates(REQUEST, this);
читайте больше здесь /questions/38768869/net-soedineniya-vyizovite-connect-ili-dozhdites-vyizova-onconnected/38768888#38768888