我正在为我的应用准备一个登录页面。我想做与推特相同的事情。他们正在帮助用户并预定义一些字段,如电子邮件,姓名和个人资料照片。我设法使用GET_ACCOUNTS
权限收到了电子邮件。
但是我无法看到我如何获得全名和个人资料图片?
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;
}
答案 0 :(得分:1)
获取帐户全名
首先将此权限添加到AndroidManifest.xml
文件
<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);
如果帐户电子邮件是 -
abc@gmail.com
然后名字是 -
ABC
答案 1 :(得分:-1)
您需要使用ContactsProvider来实现此目标。
答案 2 :(得分:-1)
将以下代码放在您的活动中。你会得到你想要的。这是获取Google用户信息的新方法。
//Global variables:
GoogleSignInOptions gso;
private GoogleApiClient mGoogleApiClient;
//inside onCreate():
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()
.requestScopes(new Scope(Scopes.PLUS_ME))
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestScopes(new Scope(Scopes.PROFILE))
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this , this )
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.build();
将以下代码放在onCreate()
之外 //to get google account info:
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
String pic_info=null;
int g;
String gender="Null";
String userid="";
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
userid=currentPerson.getId(); //BY THIS CODE YOU CAN GET CURRENT LOGIN USER ID
g=currentPerson.getGender();
gender = (g==1)?"Female":(g==0)?"Male":"Others";
}
if(acct.getPhotoUrl() != null) {
pic_info = acct.getPhotoUrl().toString();
Log.e("info", pic_info + " ");
}
Toast.makeText(getApplicationContext(),"welcome "+acct.getDisplayName(),Toast.LENGTH_LONG).show();
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("social_media","google");
editor.putString("id",userid);
editor.putString("email",acct.getEmail());
editor.putString("name",acct.getDisplayName());
editor.putString("profile_pic",pic_info);
editor.putString("gender",gender);
editor.apply();
Intent intent=new Intent(LoginActivity.this,SignOutActivity.class);
startActivity(intent);
finish();
}