LocationClient auto reconnect at `onDisconnect`
Asked Answered
L

3

6

I tried to reconnect to LocationClient when the connection gets lost (When user clear the RAM).

I tried to use this code:

private final GooglePlayServicesClient.ConnectionCallbacks mConnectionCallback = new GooglePlayServicesClient.ConnectionCallbacks() {

    @Override
    public void onDisconnected() {
        mLocationClient.removeLocationUpdates(mLocationListener);
        mLocationClient.disconnect();
        mLocationClient= null;

        mLocationClient= new LocationClient(mContext, mConnectionCallback, mConnectionFailedCallback);
        mLocationClient.connect(); // NULL POINTER EXCEPTION
    }

    @Override
    public void onConnected(Bundle bundle) {
          ...
    }
};

But I get NullPointerException inside mLocaitonClient.connect().

10-15 08:33:26.478: E/AndroidRuntime(19572): FATAL EXCEPTION: main
10-15 08:33:26.478: E/AndroidRuntime(19572): java.lang.NullPointerException
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.bh.a(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k.f(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k$e.onServiceConnected(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.l.a(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k.connect(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.location.LocationClient.connect(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.myapp.MyLocationClient$1.onDisconnected(MyLocationClient.java:92)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k.A(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.k$e.onServiceDisconnected(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.google.android.gms.internal.l$a$a.onServiceDisconnected(Unknown Source)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.app.LoadedApk$ServiceDispatcher.doDeath(LoadedApk.java:1102)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1116)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.os.Handler.handleCallback(Handler.java:615)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.os.Looper.loop(Looper.java:137)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at android.app.ActivityThread.main(ActivityThread.java:4898)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at java.lang.reflect.Method.invokeNative(Native Method)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at java.lang.reflect.Method.invoke(Method.java:511)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-15 08:33:26.478: E/AndroidRuntime(19572):    at dalvik.system.NativeStart.main(Native Method)

How can I fix it and reconnect?

Landry answered 15/10, 2013 at 5:37 Comment(0)
L
3

I found the solution! Just use Handler.

@Override
public void onDisconnected() {

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            mLocationClient.removeLocationUpdates(mLocationListener);
            mLocationClient.disconnect();
            mLocationClient = null;

            mLocationClient = new LocationClient(mContext, mConnectionCallback, mConnectionFailedCallback);
            mLocationClient.connect(); // NOW WORKING
        }
    }
}
Landry answered 17/10, 2013 at 17:23 Comment(1)
I know that this is old, but is this really required or even a good idea? From the documentation for onDisconnected() is it 'called when the client is disconnected. Applications should disable UI components that require the service, and wait for a call to onConnected(Bundle) to re-enable them.' It seems to me that manually recreating the LocationClient in onDisconnected() is not required.Schacker
M
4

an even simpler solution is to do nothing in the OnDisconnect.

public void onDisconnect(){
     //do nothing to client
}

when need to use the client simply check if is connected

if(mLocationClient.isconnected()){
     mLocationClient.connect();
}

the Google Play Services seems to reconnect nicely with out fuss.

i have used this on 4.0.4 and 4.2.2 successfully.

Marcellemarcellina answered 18/10, 2013 at 21:46 Comment(2)
But the locations updates stops, and I don't want to let it happend so I must to reconnect it again.Landry
Just register listener in the onConnected() method and you are done.Kowtow
L
3

I found the solution! Just use Handler.

@Override
public void onDisconnected() {

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            mLocationClient.removeLocationUpdates(mLocationListener);
            mLocationClient.disconnect();
            mLocationClient = null;

            mLocationClient = new LocationClient(mContext, mConnectionCallback, mConnectionFailedCallback);
            mLocationClient.connect(); // NOW WORKING
        }
    }
}
Landry answered 17/10, 2013 at 17:23 Comment(1)
I know that this is old, but is this really required or even a good idea? From the documentation for onDisconnected() is it 'called when the client is disconnected. Applications should disable UI components that require the service, and wait for a call to onConnected(Bundle) to re-enable them.' It seems to me that manually recreating the LocationClient in onDisconnected() is not required.Schacker
H
0

In the official documentation (http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html), it's written that:

public void disconnect ()

"Closes the connection to Google Play services. No calls can be made on this object after calling this method."

So you cannot call a connect() just after, you have to recreate the LocationClient object like you did the first time in order to connect again.

Highboy answered 15/10, 2013 at 7:19 Comment(5)
Still when I create a new instance of LocationClient, this exception thrown.Landry
Did you try to call the connect() in the onDisconnected() without disconnecting the location client?Highboy
I checked Google examples and on disconnect they simply do nothing or just set the client to null. When you tried my first solution, did you get the same exception?Highboy
Yea, its wasn't changed anythingLandry
@Highboy the api docs is true. But the issue is that the disconnect() is NOT called rather the onDisconnect() is called by the playservices. So u do not need to recreate the LC.Marcellemarcellina

© 2022 - 2024 — McMap. All rights reserved.