如何获取 Android 设备的主要电子邮件地址 [英] How to get the Android device's primary e-mail address

查看:30
本文介绍了如何获取 Android 设备的主要电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何获得 Android 的主要电子邮件地址(或电子邮件地址列表)?

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

据我了解,在 OS 2.0+ 上支持多个电子邮件地址,但在 2.0 以下,每台设备只能有一个电子邮件地址.

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.

作为一个友好的警告,在处理帐户、个人资料和联系人数据时,请小心并提前告知用户.如果您滥用用户的电子邮件地址或其他个人信息,可能会发生不好的事情.

您可以使用 AccountManager.getAccountsAccountManager.getAccountsByType 来获取设备上所有帐户名称的列表.幸运的是,对于某些帐户类型(包括 com.google),帐户名称是电子邮件地址.下面的示例片段.

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

请注意,这需要 GET_ACCOUNTS 权限:

Note that this requires the GET_ACCOUNTS permission:

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

有关使用 AccountManager 的更多信息,请访问 联系人管理器 SDK 中的示例代码.

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

从 Android 4.0 (Ice Cream Sandwich) 开始,您可以通过访问用户的个人资料来获取用户的电子邮件地址.访问用户个人资料有点重量级,因为它需要两个权限(更多内容见下文),但电子邮件地址是相当敏感的数据,所以这是入场的代价.

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.

以下是使用 CursorLoader 检索包含电子邮件地址的配置文件数据行的完整示例.

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

这需要 READ_PROFILEREAD_CONTACTS 权限:

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" />

这篇关于如何获取 Android 设备的主要电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆