我正在构建一个Android应用程序,我需要读取用户手机的所有联系人并存储在我的sqLite中。问题是如何获得联系,如果有3个号码有一个名称,那么如何存储3个具有该特定名称的号码请帮助我或给我一些点步骤。解决它。
答案 0 :(得分:1)
使用它:
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,null);
if (cursor != null && cursor.getCount() > 0)
{
if (cursor != null && cursor.getCount() != 0)
{
while (cursor.moveToNext())
{
String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
ContentResolver contentResolver = ctx.getContentResolver();
boolean hasPhoneNumberFlag = false;
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0)
{
Cursor cur = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.RawContacts.CONTACT_ID + "=" + Integer.valueOf(contact_id), null, null);
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
int count = 0;
if (phoneCursor != null && phoneCursor.getCount() != 0)
{
int c = phoneCursor.getCount();
while (phoneCursor.moveToNext())
{
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
}
}
if (phoneCursor != null)
{
phoneCursor.close();
}
// insert in database
ContentValues valuesFor_Contact = new ContentValues();
valuesFor_Contact.put("phonenumber", phone);
myDataBase.insert(TableName_Contacts, null, valuesFor_Contact);
}
}
}
if (cursor != null)
{
cursor.close();
}
}