1
我正在構建一個android應用程序,我需要讀取用戶手機的所有聯繫人並存儲在我的sqLite中。問題是如何獲得聯繫人,以及如果有3個號碼和一個名字,那麼如何存儲3個具有該名字的號碼請幫助我或給我一些點步驟。解決它。如何在android中的本地數據庫中存儲聯繫人?
我正在構建一個android應用程序,我需要讀取用戶手機的所有聯繫人並存儲在我的sqLite中。問題是如何獲得聯繫人,以及如果有3個號碼和一個名字,那麼如何存儲3個具有該名字的號碼請幫助我或給我一些點步驟。解決它。如何在android中的本地數據庫中存儲聯繫人?
使用本:
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();
}
}