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 :-
In the onCreate() of the MainActivity class we create a LocationClient. LocationClient is used to connect to the location services.
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.
Now before we even made the LocationClient we had already defined something known as the Location Services CallBacks.
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 :-
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.
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 :)
Related
I am restructuring my code to use ActivityResultContracts, and I've been able to recreate the old way of doing things with onActivityResult by passing in "codes" with my Intents and just passing them back from the called activity. Everything is working fine, but how would I manage to do that with App Updates, even the Google documentation is still using onActivityResult to check for app updates.
After looking at it for the past few days, it does not seem like it is possible to forgo using onActivityResult; the cancel response is only returned to that method. If you have a listener setup to check for resumed downloads, you will get a response there (as onResume is called when the calling activity is moved back to the foreground) but you only get that an update is available and the install status is unknown (which coincidentally is the same value as cancelled, and you can't do anything there as you get the same values on the initial call to check for an update). I don't see the point in deprecating a method you're still required to use, but oh well.
Using the Google Play Location and Settings API a developer can easily check if the Android device's location settings are correct.
The API also supports fixing any settings that are not correct using the startResolutionForResult method. (It calls Activity.startActivityForResult() and provides feedback of users "yes/no" choice in the activities onActivityResult() method.)
This all works fine so far, except that I am calling startResolutionForResult from a fragment. As per this question, an intent needs to called via Fragment.startActivityForResult() for the result to be given to the fragment as well.
Is there any way of getting this result to the fragment? I was half expecting a createIntentForResolutionForResult on the ResultStatus class. Ideally I don't want to modify my containing Activity.
You need to call super.onActivityResult on the Activity and call your Fragment from there. Otherwise its presence is there just for the sake of it and no work is done on the calling method which is startResolutionForResult in this case. Check the source of FragmentActivity.
I'm trying to make an android widget that shows information from a Google API, specifically the Fitness API. Right now, the Fitness API client (GoogleAPIClient object) is set up in the Main activity as a public static variable, and the onUpdate function grabs it, calls .connect(), then attempts to get information from that API. It works, but only when the app's main activity is active - If I close it, then the next time the app updates, it crashes with the error:
java.lang.RuntimeException: Unable to start receiver
Caused by: java.lang.NullPointerException
At the .connect() call in the onUpdate() function.
Is there a proper way to do this that I'm missing? How do I make it so that I can get the information every time onUpdate is called, but still handle the onConnectionFailed call - I have to start the resolution to the failed connection (usually just giving the app permissions or specifying an account) within an enclosing activity, and the AppWidgetProvider is not an activity, so I can't just put the client object in the Provider, which was my first thought.
Can I put the client inside the Provider, then launch a new activity to pass into the call to startResolutionForResult call during onConnectionFailed? If so, how would I start that intent so that it counts as an "enclosing class" - that was the error I got when I tried to put the Provider in as an argument for startResolutionForResult.
I'm new to both widgets and Google APIs, and still sort of a beginner to Android development in general though much more experienced with that than with widgets of APIs. Thanks for any help.
Is there a proper way to do this that I'm missing?
Have onUpdate() delegate work to a Service by calling startService(). Have that service talk to the Fit API. When the Service gets the results from the Fit API, it updates the app widget using AppWidgetManager, then calls stopSelf() as the Service is no longer needed.
So, I have a bunch of activities. The flow is as follows:
Main Activity: I connect to Google Plus account here, then call Show Logs, to get data from Google account.
I also have About and Setting activities.
Now if I go to About then come back to Logs, Android throws error that GoogleApiClient is not connected. So should I again call the MainActivity for connecting else should I move all of the connection code to Common or Base Activity.
I am sorry if I am not clear enough.
If the GoogleApiClient is instantiated in the Main activity, once you leave that activity it will disconnect.
For your purposes you can try having multiple instances of the GoogleApiClient across your activities (might be inefficient) but Google's interface will not require the user to sign in multiple times for each activity. Each instance will access the same state.
You can check this post for more info
Access google plus client from multiple activities
Or have try to implement the connection in an AsyncTask that will keep running in the background.
Have a look at this link
http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
I am facing problem in signing in using the google real time muleiplayer api's even after following all steps throughly as given in the documentation. Following is the link from where I have referred: https://developers.google.com/games/services/android/init.
I am not getting any error but my main activity extends BaseGameActivity when i start my game it calls onSignInFailed() automatically but i am calling the beginUserInitiatedSignIn() method from BaseGameActivity which is used for initialization of sign in on the button click none of the overridden methods gets called not even onSignInFailed() nor onConnectionFailed().
It's normal for it to call onSignInFailed on startup if have never signed in before. That's working as intended. Then, you should call beginUserInitiatedSignIn when your button is clicked, and then either onSignInFailed on onSignInSucceeded should be called.
I'd suggest putting some print statements around your code and BaseGameActivity to figure out what methods are being called to figure out what's happening.
By the way, the most common cause for sign-in errors is having a Client ID that's incorrectly set up. I'd recommend taking a look at the troubleshooting guide to see if there is a problem with your setup:
https://developers.google.com/games/services/android/troubleshooting