Question

I am new to android programming. I am planning to make an app that is location sensitive.

I am trying to use Google Play Services' Location Based APIs to detect the user's current location. For this I have been following the sample code and the details given here :-

developer.android.com- detect user's current location- Location APIs- Google Play Services

I am trying to understand the connection between the different method calls involved in detecting a user's current location. Now the above link although seems to have a lot of information, being a newbie, I find it difficult to connect the different method calls here.

So roughly what I understand form the code in the above URL is this :-

  1. In the onCreate() of the MainActivity class we create a LocationClient. LocationClient is used to connect to the location services.
  2. Now in the onStart() inside the MainActivity Class, we call locationClientObjet.connect().So it means that whenever the MainActivity of the app becomes visible, an attempt to connect to the location services is made.
  3. Now before we even made the LocationClient we had already defined something known as the Location Services CallBacks.
  4. So we implement the required interfaces and define the respective methods. So for example, the onConnected() gets called when the request to connect the client finishes successfully.

Now so far everything sounds good. The problem arises when I try to connect the above part with the below mentioned :-

  1. Now before even we had defined the Location Services CallBacks, we had defined an inner class inside the MainActivity class called, ErrorDialogFragment. This class is I guess used to create an error fragment (like an alert box) that can display error messages to the user in case apparently the connection attempt to the Location Services fails due to some reason. So there are some things that I do not understand here :-
  • What is the use of CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; The documentation in the page says :- Define a request code to send to Google Play services. This code is returned in Activity.onActivityResult. I do not understand this.
  • When is the onCreateDialog() inside the ErrorDialogFragment class get called. I do not find an explicit call to this method anywhere in the sample code mentioned on the page in the URL mentioned above.
  • What is the connection between the onConnectionFailed() and the error fragment.
  • There are 2 more methods defined :- onActivityResult() and servicesConnected(). I understand to some extent the use of servicesConnected() - it is used to see if the GooglePlay services is available. Is it a user defined method ? And is it not ding the same things that were being done inside the callback methods onConnected(), onDisConnected() and onConnectionFailed(). If not how are they different from servicesConnected() ?
  • And finally I just do not understand what is the use of onActivityResult(), what exactly are we trying to do here ?

Please pardon my ignorance. I am completely new to Android Programming and am trying to learn concepts clear and sound. Please correct me wherever I went wrong or misunderstood things. I tried looking through Vogella resources but could not find much help. Any good resources explaining details that would make my above concepts clear would be great help.

Was it helpful?

Solution

Q : What is the use of CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; The documentation in > the page says :- Define a request code to send to Google Play services. This code is > returned in Activity.onActivityResult. I do not understand this.

A : CONNECTION_FAILURE_RESOLUTION_REQUEST is the request code you have defined, If MainActivity exits, onActivityResult gets the request code, one started with. i.e . CONNECTION_FAILURE_RESOLUTION_REQUEST (or USER_DEFINED_REQUEST_CODE) This is specified by startActivityForResult(Intent,). This here is done through

connectionResult.startResolutionForResult(this,CONNECTION_FAILURE_RESOLUTION_REQUEST);

That itself calls startActivityForresult internally.

The usage is specified in the given sample code 'MainActivity.java' in the form of a comment before defining onActivityResult. This is quoted here as follows:

/* * Handle results returned to this Activity by other Activities started with * startActivityForResult(). In particular, the method onConnectionFailed() in * LocationUpdateRemover and LocationUpdateRequester may call startResolutionForResult() to * start an Activity that handles Google Play services problems. The result of this * call returns here, to onActivityResult. */

Q : When is the onCreateDialog() inside the ErrorDialogFragment class get called. I do not > find an explicit call to this method anywhere in the sample code mentioned on the page in > the URL mentioned above.

A: It is called from here :

// Show the error dialog in the DialogFragment

errorFragment.show(getSupportFragmentManager(),
                    "Location Updates");

onCreateDialog adds the dialog to the dialog cache, and the show method calls it.

Q: What is the connection between the onConnectionFailed() and the error fragment.

A: onConnectionFailed is a callback method that is called when there is an error connecting the client to the google play service. The errors list can be found at http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html in the 'Summary' section.

Now if the error occurred and it has some resolution, the error fragment will try to resolve it . Look at http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#hasResolution() AND http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)

Here ‘this’ refers to the MainActivity and CONNECTION_FAILURE_RESOLUTION_REQUEST is the user defined request code called by startActivityForResult which is here implicitly called by

connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);.

Q: There are 2 more methods defined :- onActivityResult() and servicesConnected(). I understand to some extent the use of servicesConnected() - it is used to see if the GooglePlay services is available. Is it a user defined method ? And is it not ding the same things that were being done inside the callback methods onConnected(), onDisConnected() and onConnectionFailed(). If not how are they different from servicesConnected() ?

A: Yes servicesConnected is a user defined method.

I am afraid it is not the same, it is checking if google play services is available or not through isGooglePlayServicesAvailable().

OnConnected() on the other hand will be called after isGooglePlayServicesAvailable returns true. So they act after the service is connected or disconnected , servicesConnected() just checks if it is connected or not.

Q: And finally I just do not understand what is the use of onActivityResult(), what exactly are we trying to do here ?

This i hope is clear from the previous answers :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top