Android 联系人列表

发布于 2024-08-15 06:03:38 字数 321 浏览 2 评论 0原文

任何人都可以阐明如何从 android 获取联系人列表吗?

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

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

提前致谢。

Can anyone shed a light on how to get contact list from android?.

I just want to get the same list as in the dialer app. But im getting a lots of contacts that are not on the dialer list with the code below.

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

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三五鸿雁 2024-08-22 06:03:38

试试这个片段:

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>

Try this snippet:

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 file is:

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>
一个人的旅程 2024-08-22 06:03:38

你所拥有的看起来不错。您能否详细说明“获取大量不在拨号列表中的联系人”?难道是Android在造人吗?或者您是否看到有电子邮件地址但没有电话号码的人(因此他们可能不会出现在拨号器中)?

请注意,Contacts.People 适用于 Android 1.6 及更低版本。从 Android 2.0 开始,该提供程序已被弃用,并由 ContactsContract 提供程序集取代。

What you have seems fine. Could you elaborate on "getting a lots of contacts that are not on the dialer list"? Is it that Android is making up people? Or is it that you are seeing people with email addresses but no phone numbers (who therefore might not show up in the Dialer)?

Note that Contacts.People is for Android 1.6 and below. That provider is deprecated starting with Android 2.0, replaced by the ContactsContract set of providers.

旧伤慢歌 2024-08-22 06:03:38

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

This is basic implementation of android contact list Activity.

暗喜 2024-08-22 06:03:38

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

我只想接收手机上联系人的电子邮件。 “我的联系人”组。我看到这是 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();

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

Well, thanks for the answer first. Just to shed a light on this.

I just wanted to get emails only for the contacts on my phone. The "MyContacts" group. I saw this is the group ContactList Activity uses.

I finished doing somethig like this:

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

c.close();

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

Just queried the group first and then the emails table.

抠脚大汉 2024-08-22 06:03:38

尝试使用意图去联系人列表

            startActivityForResult( new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);}

try using intent to go to the contact list

            startActivityForResult( new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文