How do you get the Android's primary e-mail address (or a list of e-mail addresses)?

It's my understanding that on OS 2.0+ there's support for multiple e-mail addresses, but below 2.0 you can only have one e-mail address per device.

 

There are several ways to do this, shown below.

As a friendly warning, be careful and up front to the user when dealing with account, profile, and contact data. If you misuse a user's email address or other personal information, bad things can happen.

Method A. Use AccountManager (API level 5+)

You can use AccountManager.getAccounts or AccountManager.getAccountsByType to get a list of all account names on the device. Fortunately, for certain account types (including com.google), the account names are email addresses. Example snippet below.

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

Note that this requires the GET_ACCOUNTS permission:

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

More on using AccountManager can be found at the Contact Manager sample code in the SDK.

Method B. Use ContactsContract.Profile (API level 14+)

As of Android 4.0 (Ice Cream Sandwich), you can get the user's email addresses by accessing their profile. Accessing the user profile is a bit heavyweight as it requires two permissions (more on that below), but email addresses are fairly sensitive pieces of data, so this is the price of admission.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Below is a full example that uses a CursorLoader to retrieve profile data rows containing email addresses.

public class ExampleActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle arguments) {
        return new CursorLoader(this,
                // Retrieve data rows for the device user's 'profile' contact.
                Uri.withAppendedPath(
                        ContactsContract.Profile.CONTENT_URI,
                        ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
                ProfileQuery.PROJECTION,

                // Select only email addresses.
                ContactsContract.Contacts.Data.MIMETYPE + " = ?",
                new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE},

                // Show primary email addresses first. Note that there won't be
                // a primary email address if the user hasn't specified one.
                ContactsContract.Contacts.Data.IS_PRIMARY + " DESC");
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        List<String> emails = new ArrayList<String>();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            emails.add(cursor.getString(ProfileQuery.ADDRESS));
            // Potentially filter on ProfileQuery.IS_PRIMARY
            cursor.moveToNext();
        }

        ...
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
    }

    private interface ProfileQuery {
        String[] PROJECTION = {
                ContactsContract.CommonDataKinds.Email.ADDRESS,
                ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
        };

        int ADDRESS = 0;
        int IS_PRIMARY = 1;
    }
}

This requires both the READ_PROFILE and READ_CONTACTS permissions:

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

 

[출처] http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
53 레이아웃이란 무엇일까요? 졸리운_곰 2016.04.30 238
52 안드로이드 네트워크/쓰레드 프로그래밍 예제 - HTTP file 졸리운_곰 2016.04.30 318
51 안드로이드 와이파이 연결시 준비된 코드 실행2 졸리운_곰 2016.04.30 171
50 안드로이드 와이파이 연결시 준비된 코드 실행 졸리운_곰 2016.04.30 423
49 Worked solution for Kivy + Admob on android 졸리운_곰 2016.04.30 580
48 Broadcast Receiver로 문자(SMS) 수신해보자 file 졸리운_곰 2016.04.29 761
» How do you get the Android's primary e-mail address (or a list of e-mail addresses)? 졸리운_곰 2016.04.29 221
46 지니모션 설치 방법 총정리 (한글키보드 + 구글플레이 설치) file 졸리운_곰 2016.04.23 1065
45 넥서스5 USB to PC 연결 뒤 아무것도 안 보인다면 file 졸리운_곰 2016.04.18 732
44 워드프레스와 모바일웹, 앱 file 졸리운_곰 2016.04.17 191
43 워드프레스 기반 안드로이드 앱 만들기 file 졸리운_곰 2016.04.17 522
42 안드로이드/Android 액티비티(Activity) 투명 처리 하기 file 졸리운_곰 2016.04.14 196
41 Audio Capture 졸리운_곰 2016.04.08 212
40 [안드로이드] 앱이 파일을 읽고 쓰는 위치 졸리운_곰 2016.03.24 879
39 안드로이드 네트워크 연결 상태 확인 졸리운_곰 2016.03.23 437
38 안드로이드 > 네트워크 연결 상태를 확인하는 ConnectivityManager 졸리운_곰 2016.03.23 652
37 [펌][안드로이드예제] 카메라로 찍은 사진(이미지파일)을 웹서버에 전송하는 프로그램예제 졸리운_곰 2016.03.12 2730
36 안드로이드 통화 녹음 소스 : android call recoder source file 졸리운_곰 2016.03.12 2118
35 안드로이드 앱 : 앱 개발시 코드에서 다른 앱 설치 및 제거 졸리운_곰 2016.03.09 295
34 [Android] 첨부파일 포함한 이메일 보내기 file 졸리운_곰 2016.03.05 911
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED