6

谁能阐明如何从 android 获取联系人列表?

我只想获得与拨号器应用程序相同的列表。但是我使用下面的代码获得了很多不在拨号列表中的联系人。

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);

提前致谢。

4

5 回答 5

6

试试这个片段:

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.SimpleCursorAdapter;

public class ContactList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null);

        startManagingCursor(cursor);

        String[] from = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER};

        int[] to = new int[] { R.id.name_entry, R.id.number_entry};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to);
        this.setListAdapter(adapter);
    }
}

XML文件是:

list_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dip">
        <TextView
            android:id="@+id/name_entry"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        android:textSize="18dip"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/number_entry"
            android:singleLine="true"
            android:ellipsize="marquee"
        android:textSize="18dip"/>
    </LinearLayout>
于 2010-10-08T08:24:46.887 回答
2

你所拥有的似乎很好。您能否详细说明“获取大量不在拨号列表中的联系人”?是安卓在造人吗?还是您看到的人有电子邮件地址但没有电话号码(因此可能不会出现在拨号器中)?

请注意,这Contacts.People适用于 Android 1.6 及更低版本。从 Android 2.0 开始,该提供程序已被弃用,取而代之的是一ContactsContract组提供程序。

于 2009-12-14T16:58:59.737 回答
1

是 android 联系人列表 Activity 的基本实现。

于 2013-11-23T06:07:21.507 回答
0

尝试使用意图转到联系人列表

            startActivityForResult( new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);}
于 2013-12-30T07:33:07.480 回答
0

嗯,先谢谢你的回答。只是为了阐明这一点。

我只想为我手机上的联系人接收电子邮件。“我的联系人”组。我看到这是 ContactList Activity 使用的组。

我完成了这样的事情:

c = cr.query(myGroupUri, mEmailsProjection, null, null, null);
....

c.close();

c = cr.query(
    Contacts.ContactMethods.CONTENT_URI,
        mContactsProjection, contactIds, null, null
);
....
c.close();

只需先查询组,然后查询电子邮件表。

于 2009-12-16T05:21:50.523 回答