I am trying to make an android app based on google-fit.
What I am trying to achieve is that I create an account for the user on my website as soon as the user chooses an account using the app.
Here is the code base that I want to build up upon (It is just the basic registration sample code) https://github.com/ishanatmuz/GoogleFitTest/tree/829051b7739ee9d8871c3ba9e5f21dfb17f4f3d7
onConnected is called when the user has succesfully signed in and provided the permissions. I am calling my own function to do further work which is basically this :
I need help figuring out how to do the step 1.
Any help will be greatly appreciated.
With the help of @Anyonymous2324 I figured out the solution. There is little more to do than what's mentioned in the answer below. So I thought it would be best for anyone who stumbles upon here in future; if I put it all together.
To get the accountName (email) or Display Name (user's name), the code required is the same as mentioned by @Anyonymous2324
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
But to get this to work there are a few things that are needed to be done.
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> to the manifest.GoogleApiClient.Builder add the Plus API like this .addApi(Plus.API)getDisplayName can work. These are .addScope(Plus.SCOPE_PLUS_LOGIN) and .addScope(Plus.SCOPE_PLUS_PROFILE)It is mentioned here that the getCurrentPerson method can return null if the required scopes are not specified or there is a network failure. Therefore it is better to check the currentPerson object before calling getDisplayName on it.
The complete code then looks like :
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mClient);
if(currentPerson != null) {
String currentPersonName = currentPerson.getDisplayName();
Log.d(TAG, currentPersonName);
logStatus(currentPersonName);
}
Since the documentation mentions that the returned value can be null in case of network error; adding permission for INTERNET seems like a good idea. But on my phone it was working without the internet connection. I guess it was taking the information from Google+ app on my phone and not going to the internet altogether, so I didn't had to use internet.
But don't take my word on it and test yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With