读取手机和SIM卡Android的所有联系人 [英] Read all contacts from phone and sim Android

查看:262
本文介绍了读取手机和SIM卡Android的所有联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

::的code ++工程。这是Eclipse问题,而code上显示的logcat输出作为意。

EDIT ::: The code works. It was a problem with Eclipse, and the code displays the output in logcat as intended.

Android的2.3.3

Android 2.3.3

我是pretty新的使用contentproviders。我只是想尝试一下如何检索从我的手机通讯录的例子。我见过几个例子,但没有工作对我来说,当我试图在我的三星手机。

I am pretty new to using contentproviders. I just want to try out an example of how to retrieve contacts from my phone. I have seen few examples, but none worked for me, when I tried it over my SAMSUNG Mobile.

下面是code,我已经使用...

Here is the code that i have used...

public class Class_Add_Contact extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_contact);

        readContacts();    
    }

    private void readContacts() {
        // TODO Auto-generated method stub

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);

        while (cur.moveToNext()) {

            String name =cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));    
            String phoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            System.out.println(name + "   " + phoneNumber);
            }
    }
}

在code似乎罚款,但目前还没有在logcat中显示任何数字。可能是什么问题?

The code seems fine, but there aren't any numbers displayed in the logcat. What could be wrong?

推荐答案

试试这个`

  Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 

    while (cursor.moveToNext()) { 
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
        String name      = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

      System.out.println(contactID + "   " + name);
    }
    cursor.close();`

返回联系人的姓名和标识(标识可能是接触表的行数)

it returns the contact's name and the id (the id is probably the row number of the contacts table)

这篇关于读取手机和SIM卡Android的所有联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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