2013-10-18 51 views
2

我用下面的代碼來獲得配置的帳戶名安卓:如何獲得配置的電子郵件帳戶地址編程

Account[] accounts = AccountManager.get(this).getAccounts(); 
     for (Account account : accounts) { 

     Log.d("Account", "Name " + account.name); 

     } 

但我需要的電子郵件ID的配置的Microsoft Exchange,因爲我們可以更改帳戶的名稱(它不需要是唯一的)。

在此先感謝

回答

4

我認爲這段代碼對你有幫助。

這裏是我的代碼:

AccountManager accManager = AccountManager.get(context); 
Account acc[] = accManager.getAccounts(); 
int accCount = acc.length; 
AppConstants.accOnDevice = new Vector<String>(); 
for(int i = 0; i < accCount; i++){ 
//Do your task here... 
} 

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

原生這不會工作工作電子郵件應用程序,允許Microsoft交換帳戶原生電子郵件應用程序允許用戶更改名稱 – Sudarshan

+0

可能是此代碼對您有用....... Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(「message/rfc822」); PackageManager pkgManager = context.getPackageManager(); 列表 activities = pkgManager.queryIntentActivities(intent,0); –

+1

這對於獲取電子郵件ID有什麼用處? – Sudarshan

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


    String gmail = null; 

    Pattern gmailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
    Account[] accounts = AccountManager.get(this).getAccounts(); 
    for (Account account : accounts) { 
     if (gmailPattern.matcher(account.name).matches()) { 
      gmail = account.name; 
     } 
    } 

    Toast.makeText(this, gmail, Toast.LENGTH_LONG).show(); 

} 
8

此代碼正常工作

public class RegisteredEmailAccounts extends Activity 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.registered_email_account); 
    final TextView accountsData = (TextView) findViewById(R.id.accounts); 

     String possibleEmail=""; 

     try{ 
       possibleEmail += "************* Get Registered Gmail Account 
            *************\n\n"; 
       Account[] accounts = 
      AccountManager.get(this).getAccountsByType("com.google"); 

       for (Account account : accounts) { 

       possibleEmail += " --> "+account.name+" : "+account.type+" , \n"; 
       possibleEmail += " \n\n"; 

       } 
      } 
      catch(Exception e) 
      { 
       Log.i("Exception", "Exception:"+e) ; 
      } 


      try{ 
       possibleEmail += "**************** Get All Registered Accounts 
         *****************\n\n"; 

       Account[] accounts = AccountManager.get(this).getAccounts(); 
       for (Account account : accounts) { 

        possibleEmail += " --> "+account.name+" : "+account.type+" , \n"; 
        possibleEmail += " \n"; 

       } 
      } 
      catch(Exception e) 
      { 
       Log.i("Exception", "Exception:"+e) ; 
      } 

     // Show on screen  
     accountsData.setText(possibleEmail); 

     Log.i("Exception", "mails:"+possibleEmail) ; 
    } 
} 
+1

很棒的回答。謝謝 – VVB

+0

這裏的完整源代碼[Android Example.com](http://androidexample.com/Get_Registered_Email_Accounts_-_Android_Example/index.php?view=article_discription&aid=110&aaid=132) – krishnan

0

試試這個代碼,肯定會爲你

AccountManager accManager = AccountManager.get(getApplicationContext()); 
Account acc[] = accManager.getAccountsByType("com.google"); 
int accCount = acc.length; 

for(int i = 0; i < accCount; i++) 
{ 
    //Do your task here...    
    Toast.makeText(getApplicationContext(),acc[i].name,Toast.LENGTH_SHORT).show(); 
} 
相關問題