- 전체
- android studio
- Android SDK
- Android NDK
- Android ADK
- Android Etc. Lib.
- Android dalvik VM
- Android 보안
- PhoneGap
- AOSP 개발
- MIT App Inventor 2
- kivy (python)
- ionic framework
- Kotlin (코틀린)
- React Native
Android SDK How do you get the Android's primary e-mail address (or a list of e-mail addresses)?
2016.04.29 21:29
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.
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
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 13 |
빠르게배우는 안드로이드 Fragment-4(Fragment간 에 데이터전달)
| 졸리운_곰 | 2020.09.19 | 155 |
| 12 |
빠르게배우는 안드로이드 Fragment-3(Fragment기초 실습)
| 졸리운_곰 | 2020.09.19 | 209 |
| 11 | 빠르게배우는 안드로이드 Fragment-2(Activity-> Fragment로 쉽게 변경) | 졸리운_곰 | 2020.09.19 | 236 |
| 10 |
빠르게배우는 안드로이드 Fragment -1 (배경)
| 졸리운_곰 | 2020.09.19 | 194 |
| 9 |
안드로이드 스튜디오 - 새로 생성한 Activity Class를 쉽게 Manifest에 등록하기.
| 졸리운_곰 | 2020.08.08 | 252 |
| 8 |
Android Studio Build시 failed linking references 해결방법
| 졸리운_곰 | 2020.05.05 | 648 |
| 7 |
[안드로이드 스튜디오] COULD NOT FIND COM.ANDROID.TOOLS.BUILD:GRADLE:3.0.0-BETA6
| 졸리운_곰 | 2020.05.05 | 125 |
| 6 |
Android Sync SQLite Database with Server using PHP and MySQL
| 졸리운_곰 | 2019.02.25 | 7762 |
| 5 |
안드로이드의 MVC, MVP, MVVM 종합 안내서
| 졸리운_곰 | 2019.01.06 | 370 |
| 4 |
Getting Started: WebView-based Applications for Web Developers
| 졸리운_곰 | 2018.09.03 | 410 |
| 3 |
Android Asynchronous Http Client
| 졸리운_곰 | 2017.07.03 | 709 |
| 2 |
android studio 2.3 의 안드로이드 레이아웃 배치 법 : Layout Editor로 UI 빌드
| 졸리운_곰 | 2017.03.09 | 534 |
| 1 |
Android Studio 2.2에서 Layout Preview 한글 깨짐 현상 해결 방법
| 졸리운_곰 | 2017.03.08 | 568 |

