2014-02-06 23 views
0

我們有一個要求,即在edittext中顯示主要Gmail帳戶。但是我收到了所有在我的手機中登錄的電子郵件(Gmail)帳戶。在我的手機中獲取所有gmail中的主要gmail ID

我正在使用下面的代碼。

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
       Account[] accounts = AccountManager.get(MainActivity.this).getAccountsByType("com.google"); 
       if(accounts.length>1){ 
        for (Account account : accounts) { 
         if (emailPattern.matcher(account.name).matches()) { 
          String possibleEmail = account.name; 
          Log.d("email::","email::"+possibleEmail+"----"+account.type); 
         } 
        } 
       }else{ 
        Log.d("email::","email::no mails found"); 
       } 

請幫我找出主電子郵件ID。

感謝

回答

1

試試這個: -

private String getEmailID() { 
     AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE); 
     Account[] list = manager.getAccounts(); 

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

     mStr_email = gmail; 
     Log.d(KEY_TAG, "EMAIL" + mStr_email); 

     return mStr_email; 
    } 

代碼來檢查Internet連接是否爲開: -

InternetConnection ic = new InternetConnection(getApplicationContext()); 
      isInternetPresent = ic.isConnectingToInternet(); 
      if (!isInternetPresent) { 

       Utilities.showToast(SplashActivity.this, 
         "Switch on Internet Connection"); 
       startActivity(new Intent(Settings.ACTION_SETTINGS)); 
      } else { 
       Log.d(KEY_TAG, "Check attendane "); 
      } 
+0

感謝您的回覆,這裏是什麼考勤();?而且我也沒有得到主要的電子郵件。只有所有電子郵件中的電子郵件數量最多。 :( – user2432358

+0

它什麼都沒有。 看到更新的awnser – Namy

+0

你也可以在這裏檢查網絡連接.. – Namy