Android電話でログインしているGoogleアカウントを取得するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/4038333

  •  27-09-2019
  •  | 
  •  

質問

Androidアプリケーションを開発しているので、電話で使用されているGoogleアカウントを取得する必要があります。 C2DMのためにこれをやりたいのですが、すでにログインしている場合は、ユーザーにGoogleメールアカウントに入力するようにユーザーに依頼したくありません。それを行う方法はありますか?

役に立ちましたか?

解決

このようなものは機能するはずです:

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
String gmail = null;

for(Account account: list)
{
    if(account.type.equalsIgnoreCase("com.google"))
    {
        gmail = account.name;
        break;
    }
}

そして、あなたはあなたのマニフェストで次の許可を必要とします:

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

Android 6以降をサポートする場合は、「実行時にアクセス許可を要求する」ことを忘れないでくださいhttps://developer.android.com/training/permissions/requesting.html

私はこれをメモリから書いたので、少し微調整する必要があるかもしれません。どうやら電子メールアドレスなしで今すぐ登録することが可能なので、実際に電子メールアドレスであることを確認するために、データにregexingを実行する可能性があります(@gmailまたは@googlemailが含まれていることを確認してください)

他のヒント

私は取得するために範囲以下を試しました 電子メールアドレスユーザー名

モバイルでGoogleアカウントを入手してください

 public String getMailId() {
        String strGmail = null;
        try {
            Account[] accounts = AccountManager.get(this).getAccounts();
            Log.e("PIKLOG", "Size: " + accounts.length);
            for (Account account : accounts) {

                String possibleEmail = account.name;
                String type = account.type;

                if (type.equals("com.google")) {

                    strGmail = possibleEmail;
                    Log.e("PIKLOG", "Emails: " + strGmail);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
             strGmail = null;
        }

        return strGmail;
    }

モバイルでGoogleアカウントのユーザー名を取得します

 public String getUsername() {
    List<String> possibleEmails = null;
    try {
        AccountManager manager = AccountManager.get(this);
        Account[] accounts = manager.getAccountsByType("com.google");
        possibleEmails = new LinkedList<>();

        for (Account account : accounts) {
            // TODO: Check possibleEmail against an email regex or treat
            // account.name as an email address only for certain account.type
            // values.
            possibleEmails.add(account.name);
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (possibleEmails != null) {
            possibleEmails.clear();
        }
    }

    if (possibleEmails != null) {
        if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
            String email = possibleEmails.get(0);
            String[] parts = email.split("@");
            if (parts.length > 0 && parts[0] != null) {
                return parts[0];

            } else {
                return null;
            }
        } else {
            return null;
        }
    } else {
        return null;
    }
}

メインフェストファイルにアクセス許可を宣言します。

  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top