读取手机和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)