[英]How to find which account (email) has user used to log into my google-fit based android app?
I am trying to make an android app based on google-fit. 我正在尝试制作一个基于google-fit的Android应用程序。
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 这是我想要构建的代码库(它只是基本的注册示例代码) https://github.com/ishanatmuz/GoogleFitTest/tree/829051b7739ee9d8871c3ba9e5f21dfb17f4f3d7
onConnected is called when the user has succesfully signed in and provided the permissions. 当用户成功登录并提供权限时,将调用onConnected 。 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. 我需要帮助搞清楚如何执行第1步。
Any help will be greatly appreciated. 任何帮助将不胜感激。
With the help of @Anyonymous2324 I figured out the solution. 在@ Anyonymous2324的帮助下,我找到了解决方案。 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 要获取accountName(电子邮件)或显示名称(用户名),所需的代码与@ 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. <uses-permission android:name="android.permission.GET_ACCOUNTS" />
来添加访问设备帐户的<uses-permission android:name="android.permission.GET_ACCOUNTS" />
。 GoogleApiClient.Builder
add the Plus API like this .addApi(Plus.API)
GoogleApiClient.Builder
添加这样的Plus API .addApi(Plus.API)
getDisplayName
can work. getDisplayName
可以工作。 These are .addScope(Plus.SCOPE_PLUS_LOGIN)
and .addScope(Plus.SCOPE_PLUS_PROFILE)
.addScope(Plus.SCOPE_PLUS_LOGIN)
和.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. 这里提到的是,如果未指定所需的范围或网络出现故障,则
getCurrentPerson
方法可以返回null
。 Therefore it is better to check the currentPerson
object before calling getDisplayName
on it. 因此,最好在调用
currentPerson
对象之前调用getDisplayName
。 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; 由于文档提到在网络错误的情况下返回的值可以为null; adding permission for INTERNET seems like a good idea.
添加INTERNET权限似乎是一个好主意。 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.
我想它是从我的手机上的Google+应用程序中获取信息而不是完全上网,所以我不必使用互联网。
But don't take my word on it and test yourself. 但是不要接受我的话并测试自己。
On your onConnected()
method, you can get it with: 在
onConnected()
方法上,您可以使用:
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.