android獲取google郵箱

Java:java

/**
     * 獲取當前用戶郵箱
     */
    public static 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;
      }

還須要申請權限AndroidManifest.xml:android

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


Ok了~ 測試經過的喲~正則表達式

再放一個郵箱的正則:ide

/**
     * 郵箱判斷正則表達式
     * @param eMAIL1
     * @return
     */
    public static boolean MatchEmail(String eMAIL1) {
         Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
         Matcher mc = pattern.matcher(eMAIL1);
         return mc.matches();
     }
相關文章
相關標籤/搜索