如何从设备获取有效的Facebook帐户?

时间:2014-02-19 05:51:06

标签: android android-facebook

我正在使用此代码部分,但它返回null。

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
accounts = AccountManager.get(Login.this).getAccountsByType("com.facebook");
String possibleEmail;

for (Account account : accounts) 
{
    if (emailPattern.matcher(account.name).matches()) 
    {
        possibleEmail = account.name;
        Log.e("Email", possibleEmail);
    }
}

但它适用于(“com.google”)。

1 个答案:

答案 0 :(得分:0)

最好打印出所有类型并查看需要用来访问的内容,因为按类型名称提取它们需要完全匹配。

    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+

    AccountManager acMng = AccountManager.get(this.getApplicationContext());
    if(acMng!=null) {           

    Account[] accounts = acMng.getAccounts();
    String possibleEmail;

    if(accounts!= null) {
    for (Account account : accounts) 
    {
         Log.e(( account.name , account.type );
        if (emailPattern.matcher(account.name).matches()) 
        {
            possibleEmail = account.name;
            Log.e(("Email :",  possibleEmail);
        }
    }
    }

在我的测试设备类型中,facebook是 - com.facebook.auth.login

所以我认为需要:

 accounts = AccountManager.get(Login.this).getAccountsByType("com.facebook.auth.login");