【问题标题】:Why do I get null from retrieving the user's gmail?为什么我从检索用户的 gmail 中得到空值?
【发布时间】:2016-04-20 16:55:58
【问题描述】:

使用以下代码检索用户的 Google 帐户 gmail,我想知道为什么我从像我这样的设备(当然在我的 gmail 上运行)得到 null,而它在我妈妈的设备上工作:

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

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

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

显然,这是因为我的设备...但它不能是唯一返回 null 的设备,所以我不知道这是否是使用应用内验证有效负载时唯一字符串令牌的好方法计费。

哦,这是我在帐户与同步设置中看到的屏幕截图:

...这里有什么我遗漏的吗?

【问题讨论】:

    标签: android email in-app-billing accountmanager


    【解决方案1】:

    基于文档Set the developer payload string。发出购买请求时,您不应在有效负载字符串中使用用户的电子邮件地址,因为该地址可能会更改。

    您应该传入一个字符串令牌,以帮助您的应用程序识别进行购买的用户,以便您以后可以验证这是该用户的合法购买。对于消耗品,您可以使用随机生成的字符串,但对于非消耗品,您应该使用唯一标识用户的字符串。

    【讨论】:

    • 感谢您的回复,但就是这样。对于非消耗品,我不知道如何为每个用户生成一个唯一的字符串令牌,而令牌在他们的所有设备上运行......因此我检索电子邮件地址然后立即将它们加密为令牌。
    • 对于在用户的所有设备上运行的令牌,您有什么建议?
    【解决方案2】:

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

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

    Android 8.0 弃用了 LOGIN_ACCOUNTS_CHANGED_ACTION。应用应改为使用 addOnAccountsUpdatedListener() 在运行时获取有关帐户的更新。

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

    也许你可以试试这个

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2021-04-15
      相关资源
      最近更新 更多