Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement onConnectionFailed() and onDisconnected() for a location service

I'm using a Service which implements com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks and com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener

The service is supposed to upload location updates to a server every minute when it's running. I've googled for tutorials but I can't find any good ones for my scenario.

The methods that I don't know how to implement are:

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

    ...
}

There are some tutorials suggesting to start an activity with connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); but since this is a Service (not an Activity) I can't use this method. I thought of sending the connectionResult in an Intent to an activity but ConnectionResult is not serializable.

and

@Override
public void onDisconnected() {

    ...
}

Should I just call LocationClient#connect() again in here?

like image 408
Peter Warbo Avatar asked Mar 11 '14 14:03

Peter Warbo


1 Answers

1. In onConnectionFailed():
For "I thought of sending the connectionResult in an Intent to an activity but ConnectionResult is not serializable." I think it can work without that in the following way:

You can return the int ; ErrorCodes or Constants value of ConnectionResult and depending on that value, you can assign further actions, just pass the control to your activity/application.
From:
http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html

2. In onDisconnected(),
You can notify the user that "you are no longer receiving location updates" or another custom message depending on your app which would prompt the user to take an action.
If your service enters onDisconnected() for some reason we aren't aware of, calling locationClient.connect() again may not help.

In both scenarios, the idea is to pass the control to your app so somewhere in your activity, it can work as a decision point for the control flow.

Also, you have mentioned "...but I can't find any good ones for my scenario." so describing your requirement a bit precisely may help you more. Will edit my answer accordingly.

If its sending location updates to the server, then passing the control back to your activity/app and prompt user also for action would also help. (eg. Check Settings for Location access services enabled)


Current scenario for anybody looking up this question/answer
LocationClient is no longer found under com.google.android.gms.location, refer:
Android play services 6.5: LocationClient is missing

like image 196
Pararth Avatar answered Nov 05 '22 05:11

Pararth