2010-10-27 12 views

risposta

44

Qualcosa del genere dovrebbe funzionare:

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

e sarà necessario l'autorizzazione seguente nel vostro manifesto:

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

Ricordarsi di 'Richiesta di permessi in fase di esecuzione' se sostenete Android 6 e più avanti https://developer.android.com/training/permissions/requesting.html

Ho scritto questo dalla memoria in modo che potrebbe essere necessario un piccolo ritocco. Apparentemente è possibile registrarsi ora senza un indirizzo email, quindi forse fare un po 'di regexing sui dati per assicurarsi che sia effettivamente un indirizzo email (assicurati che contenga @gmail o @googlemail)

+0

grazie! proprio quello di cui avevo bisogno. – fanar

+0

Chiunque sia a conoscenza di soluzioni simili compatibili con Android 1.6? –

+0

Trovato una risposta qui: http://stackoverflow.com/questions/3360926/get-main-gmail-account-username-in-android-2-0 –

0

Ho provato di seguito scope per ottenere l'indirizzo email e nome utente

Get account Google sul cellulare

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

Get Google account nome utente nel tuo cellulare

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

permessi di dichiarazione al file Mainfest.

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