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

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

问题描述

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

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

我的理解是,在操作系统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.

推荐答案

有几个

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

您可以使用 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 权限:

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

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






5/6/2012—添加Android 4.0 +的个人资料访问信息

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

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