为什么我从检索用户的gmail时得到空值? [英] Why do I get null from retrieving the user's gmail?

查看:197
本文介绍了为什么我从检索用户的gmail时得到空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码检索用户的Google帐户gmail,我想知道为什么我会从我的设备(当然是在我的gmail上运行)中获取空值,而在我妈妈的设备上却无法使用

With the following code that retrieves the user's Google account, gmail, I was wondering why I get null from devices like mine (that of course runs on my gmail), whereas it works on my mom's devices:

public class MainActivity extends AppCompatActivity {

    final String TAG = "Sample2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String test = getEmail(getApplicationContext());
        Log.d(TAG, "Email is: " + test);
        TextView emailTxt = (TextView)findViewById(R.id.emailTxt);
        emailTxt.setText(test);
    }

    private 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;
    }
}

我还将以下权限包含在清单文件中:

I also included the following permission into my Manifest file:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

很明显,这是因为我的设备而已...但是它不可能是唯一返回的设备null,因此我不知道在通过应用内结算验证有效负载时,这是否是唯一字符串令牌的好方法。

Clearly, it's because of my device... But it can't be the only device that's returning null, so I don't know if this is a good approach for a unique string token when verifying payload with in-app billing.

哦,这是截图我在帐户和帐户中看到的内容同步设置:

Oh, and here's a screenshot of what I see in my Accounts & Sync settings:

。 ..这里有什么我想念的吗?

... Is there anything I'm missing here?

推荐答案

> https://developer.android.com/about/versions/oreo/android-8.0-changes.html

帐户访问和可发现性
在Android 8.0(API级别26)中,除非身份验证者拥有帐户或用户授权,否则应用将无法再访问用户帐户该访问。 GET_ACCOUNTS权限不再足够。要被授予访问帐户的权限,应用程序应使用AccountManager.newChooseAccountIntent()或特定于身份验证器的方法。访问帐户后,应用程序可以调用AccountManager.getAccounts()来访问它们。

Account access and discoverability In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission is no longer sufficient. To be granted access to an account, apps should either use AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting access to accounts, an app can can call AccountManager.getAccounts() to access them.

Android 8.0不赞成使用LOGIN_ACCOUNTS_CHANGED_ACTION。应用程序应该改为使用addOnAccountsUpdatedListener()在运行时获取有关帐户的更新。

Android 8.0 deprecates LOGIN_ACCOUNTS_CHANGED_ACTION. Apps should instead use addOnAccountsUpdatedListener() to get updates about accounts during runtime.

有关为帐户访问和可发现性而添加的新API和方法的信息,请参阅中的帐户访问和可发现性。本文档的新API部分

For information about new APIs and methods added for account access and discoverability, see Account Access and Discoverability in the New APIs section of this document

也许您可以尝试

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private static final int PICK_ACCOUNT_REQUEST = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Intent googlePicker = AccountManager.newChooseAccountIntent(null, null,
            new String[] { "com.google"}, true, null, null, null, null);
    startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST);
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if (requestCode == PICK_ACCOUNT_REQUEST && resultCode == RESULT_OK) {
        String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        Log.d(TAG, "Account Name=" + accountName);
        String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
        Log.d(TAG, "Account type=" + accountType);

        AccountManager accountManager = AccountManager.get(this);
        Account[] accounts = accountManager.getAccounts();
        for (Account a :
                accounts) {
            Log.d(TAG, "type--- " + a.type + " ---- name---- " + a.name);
        }
    }
}

}

这篇关于为什么我从检索用户的gmail时得到空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆