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

查看:268
本文介绍了如何获得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.getAccounts AccountManager.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 可以在<一个被发现href="http://developer.android.com/resources/samples/ContactManager/src/com/example/android/contactmanager/ContactAdder.html">Contact经理样品code SDK中。

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

由于Android的4.0(冰淇淋三明治),你可以通过访问他们的个人资料得到了用户的电子邮件地址。访问用户配置文件是有点重量级的,因为它需要两个权限(低于更多),但电子邮件地址是相当敏感的数据块,所以这是门票价格。

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_PROFILE READ_CONTACTS 权限:

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


2012年5月6日&mdash;关于Android 4.0的配置文件访问增加了信息+

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

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