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
}
I found the same problem in a massively deployed app, at the google developer console crash report.
I suspect the problem was caused by my app trying to reconnect client in onDisconnected()
method, though could not verify that while waiting for next release schedule for my app.
Another stackoverflow article gave me excellent hints about the problem by suggesting (1) use Handler
to avoid calling connect()
method in the callback itself, (2) avoid re-using the LocationClient
object and just destroy old one and create a new one.
I wish this be helpful.