Why do I get null from retrieving the user's gmail?

寵の児 提交于 2019-12-12 01:07:21

问题


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" />

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?


回答1:


Based on the document Set the developer payload string. When making purchase requests, you should not use the user's email address in the payload string, since the address may change.

You should pass in a string token that helps your application to identify the user who made the purchase, so that you can later verify that this is a legitimate purchase by that user. For consumable items, you can use a randomly generated string, but for non- consumable items you should use a string that uniquely identifies the user.




回答2:


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

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 deprecates LOGIN_ACCOUNTS_CHANGED_ACTION. Apps should instead use addOnAccountsUpdatedListener() to get updates about accounts during runtime.

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

Maybe you can try this

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

}



来源:https://stackoverflow.com/questions/36750402/why-do-i-get-null-from-retrieving-the-users-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!