从“ME”获取所有信息联系android

时间:2013-12-18 11:17:09

标签: android contacts

在Android联系人中,在联系人列表的头部有一个名为“ME”的帐户,我将所有个人信息放入其中,如何在我的应用程序中获取这些信息,我可以从帐户中获取电子邮件帐户此

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

但在我的应用程序中,我需要所有可用的数据,如姓名,手机号码,家庭电话号码,电子邮件。 有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

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,

                //Don't select anything here null will return all available fields

                null,
                null,                   
                null);
    }
    @Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        ArrayList<String> DataArray = new ArrayList<String>();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
                    //here where you get your data and its type
            TypeName=cursor.getString(ProfileQuery.ADDRESS);//this will give you field name
            Data=cursor.getString(ProfileQuery.NUMBER);//this will give you field data

            cursor.moveToNext();
        }
    }
    @Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
    }
private interface ProfileQuery {
        String[] PROJECTION = {
                ContactsContract.Contacts.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Email.ADDRESS  ,
                ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Organization.DATA3,

        };

        int ADDRESS = 0;
        int NUMBER = 1;
    }

已编辑:

在此链接中 How to get the Android device's primary e-mail address

在onCreateLoader中他只指定了电子邮件,所以只需将其删除并修改界面即可获得所需的结果