获取 Gmail 个人资料图片和全名 [英] Get gmail profile image and full name
本文介绍了获取 Gmail 个人资料图片和全名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为我的应用准备登录页面.我想做和推特一样的事情.他们正在帮助用户并预定义一些字段,如电子邮件、姓名和个人资料图片.我设法使用 GET_ACCOUNTS
权限获取了电子邮件.
I am preparing a login page for my app. I wanted to do the same thing that twitter is doing. They are helping the user and predefining some fields like email, name and profile pic. I managed to get the email using the GET_ACCOUNTS
permission.
但是我看不到我是如何获得全名和个人资料照片的?
However I cant see how I get the full name and profile pic?
public static String getEmail(Context context)
{
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null)
{
return null;
}
else
{
return account.name;
}
}
private static Account getAccount(AccountManager accountManager)
{
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0)
{
account = accounts[0];
}
else
{
account = null;
}
return account;
}
推荐答案
获取账户全名
首先将此权限添加到AndroidManifest.xml
文件中
First Add this permission into AndroidManifest.xml
file
<uses-permission
android:name="android.permission.GET_ACCOUNTS"/>
Java 代码
public static Account getAccount(AccountManager accountManager) {
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
调用getAccount();
Account account = getAccount(AccountManager.get(context));
String accountName = account.name;
String fullName = accountName.substring(0,accountName.lastIndexOf("@"));
emailEditText.setText(accountName);
fullNameEditText.setText(fullName);
如果帐户电子邮件是 -
If Account Email is -
abc@gmail.com
abc@gmail.com
那么名字是 -
ABC
这篇关于获取 Gmail 个人资料图片和全名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文