如何獲取Android中聯繫人的所有名稱並將其放入字符串數組中?android獲取所有聯繫人
回答
試試這個太,
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
如果您需要更多的基準裝置是指該鏈接Read ContactList
感謝您的幫助,我如何顯示ListView中的所有名稱?(我可以在另一個窗口中查看我手機中的所有聯繫人姓名)?謝謝 –
http://stackoverflow.com/questions/5165599/how-to-show-phone-contacts-in-a-listview這是你正在尋找的東西..我知道你會接受它作爲答案,如果我把複製粘貼代碼在這裏... –
爲什麼每個人都包含'if(cur.getCount()> 0){'?遊標應該在*第一項之前開始。 – chacham15
Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String aNameFromContacts[] = new String[contacts.getCount()];
String aNumberFromContacts[] = new String[contacts.getCount()];
int i = 0;
int nameFieldColumnIndex = contacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int numberFieldColumnIndex = contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
while(contacts.moveToNext()) {
String contactName = contacts.getString(nameFieldColumnIndex);
aNameFromContacts[i] = contactName ;
String number = contacts.getString(numberFieldColumnIndex);
aNumberFromContacts[i] = number ;
i++;
}
contacts.close();
其結果將是aNameFromContacts陣列充分接觸。另外,還要確保您已在main.xml中
非常感謝我會嘗試一下,如果我還有其他問題,我會問你 –
感謝您的幫助,我如何顯示ListView中的所有名稱?(我可以在另一個窗口中看到我手機中的所有聯繫人姓名) ?謝謝 –
http://stackoverflow.com/questions/5165599/how-to-show-phone-contacts-in-a-listview這是你正在尋找的東西..我知道你會接受它作爲答案,如果我把複製粘貼代碼在這裏... –
public class MyActivity extends Activity
implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int CONTACTS_LOADER_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(CONTACTS_LOADER_ID,
null,
this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created.
if (id == CONTACTS_LOADER_ID) {
return contactsLoader();
}
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
//The framework will take care of closing the
// old cursor once we return.
List<String> contacts = contactsFromCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
}
private Loader<Cursor> contactsLoader() {
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI; // The content URI of the phone contacts
String[] projection = { // The columns to return for each row
ContactsContract.Contacts.DISPLAY_NAME
} ;
String selection = null; //Selection criteria
String[] selectionArgs = {}; //Selection criteria
String sortOrder = null; //The sort order for the returned rows
return new CursorLoader(
getApplicationContext(),
contactsUri,
projection,
selection,
selectionArgs,
sortOrder);
}
private List<String> contactsFromCursor(Cursor cursor) {
List<String> contacts = new ArrayList<String>();
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts.add(name);
} while (cursor.moveToNext());
}
return contacts;
}
}
添加
<uses-permission android:name="android.permission.READ_CONTACTS" />
,不要忘記
<uses-permission android:name="android.permission.READ_CONTACTS" />
得到公正的代碼行數的聯繫人列表
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(),name, Toast.LENGTH_LONG).show();
}
phones.close();
獲取聯繫人信息,照片聯繫人,照片uri並轉換爲類型
1)。樣品分類型號:
public class ContactModel {
public String id;
public String name;
public String mobileNumber;
public Bitmap photo;
public Uri photoURI;
}
2)。獲取聯繫人並轉換爲模型
public List<ContactModel> getContacts(Context ctx) {
List<ContactModel> list = new ArrayList<>();
ContentResolver contentResolver = ctx.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id));
Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Bitmap photo = null;
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
while (cursorInfo.moveToNext()) {
ContactModel info = new ContactModel();
info.id = id;
info.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
info.mobileNumber = cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.photo = photo;
info.photoURI= pURI;
list.add(info);
}
cursorInfo.close();
}
}
cursor.close();
}
return list;
}
真棒函數,只需要添加一行以完成第一個while子句後關閉遊標。非常感謝 –
- 1. 如何獲取所有android聯繫人但沒有SIM卡上的聯繫人
- 2. Android - 從所有來源獲取所有聯繫人
- 3. ews resolvename獲取所有聯繫人
- 4. Android獲取聯繫人列表,但沒有SIM卡聯繫人
- 5. Android獲取ArrayList中的所有聯繫人電話號碼
- 6. 獲取Android中的所有聯繫人數據 - 多個URI
- 7. 獲取聯繫人的所有號碼(電話)? (在Android 2.1)
- 8. 從Android設備獲取所有聯繫人的錯誤
- 9. 如何在Android程序中獲取所有Sim聯繫人?
- 10. 如何獲取Android聯繫人列表上聯繫人的ID?
- 11. 從Android聯繫人獲取聯繫人姓名和姓氏
- 12. 如何從android中的聯繫人獲取聯繫人號碼?
- 13. 如何獲取所有聯繫人及其所有屬性
- 14. 獲取聯繫人
- 15. 如何獲取與給定聯繫人合併的所有聯繫人
- 16. android獲取聯繫人的暱稱
- 17. Android聯繫人列表獲取地址
- 18. Android獲取聯繫人手機圖像
- 19. 獲取Android聯繫人排序偏好
- 20. Xamarin android獲取地址聯繫人
- 21. 在Xamarin中獲取Android聯繫人
- 22. Android - 無法獲取聯繫人照片?
- 23. 獲取聯繫人在Android的
- 24. 如何在Android中獲取聯繫人
- 25. Android聯繫人 - 獲取電話號碼
- 26. 在android中獲取當前聯繫人
- 27. 如何從Android獲取whatsapp聯繫人?
- 28. 在android中獲取聯繫人
- 29. android獲取聯繫人的名字
- 30. Android聯繫人 - Android聯繫人的所有帳戶類型是什麼?
你試過使用http://developer.android.com/reference/android/provider/ContactsContract.html? – sandrstar
[檢查此](http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/3/) – Harish
可能重複[如何在Android中獲取手機通訊錄](http:// stackoverflow .com/questions/4133163/how-to-get-phone-contacts-in-android) – NGLN