I am using Google Play Services LocationClient in Android. I do not have any problems getting location and location updates. However, when my app gets into the background, sometimes the app is stopped by android and this error y throwed:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:792)
at java.util.HashMap$KeyIterator.next(HashMap.java:819)
at com.google.android.gms.internal.l$a$a.onServiceDisconnected(Unknown Source)
at android.app.LoadedApk$ServiceDispatcher.doDeath(LoadedApk.java:1102)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1116)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4929)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
I have tried to get rid off this error several times, this is how my code for locationClient is working right now:
@Override
public void onConnected(Bundle arg0) {
counter = 0;
if (getLocationClient().isConnected()) {
getLocationClient().requestLocationUpdates(getLocationRequest(),
providerListener);
;
listen();
} else if (!getLocationClient().isConnecting()){
getLocationClient().connect();
}
}
@Override
public void onDisconnected() {
if(_locationClient!= null && !_locationClient.isConnected() && !_locationClient.isConnecting()) {
try {
connectLocationClient();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The methos getLocationClient and getLocationRequest are just to make sure this objects are not null:
private LocationClient getLocationClient() {
if (_locationClient == null) {
_locationClient = new LocationClient(context, this, this);
}
return _locationClient;
}
private LocationRequest getLocationRequest() {
if (_locRequest == null) {
_locRequest = new LocationRequest();
_locRequest.setInterval(fixTime);
_locRequest
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
return _locRequest;
}
Any idea of what may be happening?
Thanks!
Edit:
Like shr pointed, calling connect() in onDisconnected was probably causing this, so I did remove that from there. Also calling requestLocationUpdates() in onConnected() can cause some troubles, so instead:
@Override
public void onConnected(Bundle arg0) {
requestUpdates();
}
public void requestUpdates(){
getLocationClient().requestLocationUpdates(getLocationRequest(),this);
}
@Override
public void onDisconnected() {
//Use a handler instead to reconnect
}