2010-03-31 162 views
33

我有android.permission.READ_OWNER_DATA但我找不到任何可靠的代碼,可以解釋如何讀取設備所有者的電子郵件地址。我怎樣才能做到這一點?如何獲取Android用戶的電子郵件地址?

+1

看到這個問題:http://stackoverflow.com/questions/2112965/how-to-get-the-android- devices-primary-e-mail-address – 2010-03-31 21:13:01

+1

我的代碼適用於2個Android版本,但我希望它能幫助別人。 它獲取所有者姓名,電子郵件和電話(用戶除syncyncization帳戶以外可能有電子郵件)。 在android 2.3上測試https://gist.github.com/3904299 – Jehy 2012-10-17 08:10:00

+4

爲什麼好的問題關閉?我開始越來越多地看到這一點。 – 2013-11-07 15:43:14

回答

90

爲什麼你想那樣做?

import android.accounts.Account; 
import android.accounts.AccountManager; 
import android.content.Context; 

/** 
* This class uses the AccountManager to get the primary email address of the 
* current user. 
*/ 
public class UserEmailFetcher { 

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

在你AnroidManifest.xml

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

這看起來很不錯...但我需要一些適用於Android 1.5的東西?你的袖子裏有沒有老班級的API? – kape123 2010-03-31 21:29:07

+2

這不適用於Android 1. *恐怕。如果它有任何的安慰,你仍然可以在一個目標爲1.5的應用程序中使用它,並且乾淨地檢測功能何時不可用以及 嘗試使用String name = UserEmailFetcher.getEmail(this); }趕上(的VerifyError E){// 偏偏如果的AccountManager不可用(例如1.x中) } 您需要 <使用-SDK安卓的minSdkVersion = 「3」/>在 您表現。 – 2010-03-31 21:36:05

+0

這是隱藏的,你必須使用反射才能得到它。不要,如果你真的需要。 – alexanderblom 2010-04-01 01:10:13

0

上棉花糖版本

btn_click =(按鈕)findViewById(R.id.btn_click)工作;

btn_click.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) 
    { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
     { 
      int permissionCheck = ContextCompat.checkSelfPermission(PermissionActivity.this, 
        android.Manifest.permission.CAMERA); 
      if (permissionCheck == PackageManager.PERMISSION_GRANTED) 
      { 
       //showing dialog to select image 
       String possibleEmail=null; 

       Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
       Account[] accounts = AccountManager.get(PermissionActivity.this).getAccounts(); 
       for (Account account : accounts) { 
        if (emailPattern.matcher(account.name).matches()) { 
         possibleEmail = account.name; 
         Log.e("keshav","possibleEmail"+possibleEmail); 
        } 
       } 

       Log.e("keshav","possibleEmail gjhh->"+possibleEmail); 
       Log.e("permission", "granted Marshmallow O/S"); 

      } else {      ActivityCompat.requestPermissions(PermissionActivity.this, 
         new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, 
           android.Manifest.permission.READ_PHONE_STATE, 
           Manifest.permission.GET_ACCOUNTS, 
           android.Manifest.permission.CAMERA}, 1); 
      } 
     } else { 

//下再棉花糖

   String possibleEmail=null; 

       Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
       Account[] accounts = AccountManager.get(PermissionActivity.this).getAccounts(); 
       for (Account account : accounts) { 
        if (emailPattern.matcher(account.name).matches()) { 
         possibleEmail = account.name; 
         Log.e("keshav","possibleEmail"+possibleEmail); 
       } 
       Log.e("keshav","possibleEmail gjhh->"+possibleEmail); 
     } 
    } 
}); 

+0

** ** – 2017-07-12 11:58:15

相關問題