Android bekommt alle Kontakte

Wie bekomme ich alle Namen der Kontakte in meinem Android und stelle sie in Array von Strings?

  • Senden von E-Mails in Android mit JavaMail API ohne Verwendung der Standard- / eingebauten App
  • Falscher Java-Compiler bei Einbeziehung eines Java-Moduls als Abhängigkeit in Android Studio
  • Bearbeiten Konfiguration benutzerdefinierte spezifische Instrumentierung Läufer fehlende Eigenschaft
  • Holen Sie sich aktuelle Wallpaper absolute Weg?
  • Speichergrenze im Roh- und Asset-Ordner in Android
  • Qt5 QGeoPositionInfoSource :: createDefaultSource () stürzt auf Android 5.0 ab
  • Wie teste ich android referral tracking?
  • Aktivität nicht sichtbar in Recents, wenn App im Hintergrund ist
  • 'Adb-Geräte' mit Dummy-Gerät
  • Capture Image, während Device mit Passwort gesperrt ist
  • Wie kann ListView nicht anklickbar sein
  • Steuern von Compiler-Flags im Android NDK?
  • 5 Solutions collect form web for “Android bekommt alle Kontakte”

    Versuche das auch,

    ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (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)); Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show(); } pCur.close(); } } } 

    Wenn Sie mehr Referenzmittel benötigen, verweisen Sie auf diesen Link Lesen Sie Kontaktliste

    Holen Sie sich die Kontaktliste nur in wenigen Codezeilen

     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(); 
     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; } } 

    und vergiss nicht

     <uses-permission android:name="android.permission.READ_CONTACTS" /> 

    Holen Sie sich Kontaktinformationen, Fotokontakte, Foto uri und konvertieren in Klasse Modell

    1). Beispiel für Klassenmodell:

      public class ContactModel { public String id; public String name; public String mobileNumber; public Bitmap photo; public Uri photoURI; } 

    2). Kontakte erhalten und in Modell umwandeln

      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(); } } } return list; } 
     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(PhoneLookup.DISPLAY_NAME); int numberFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.NUMBER); while(contacts.moveToNext()) { String contactName = contacts.getString(nameFieldColumnIndex); aNameFromContacts[i] = contactName ; String number = contacts.getString(numberFieldColumnIndex); aNumberFromContacts[i] = number ; i++; } contacts.close(); 

    Das Ergebnis wird aNameFromContacts Array voller Kontakte. Stellen Sie außerdem sicher, dass Sie hinzugefügt haben

     <uses-permission android:name="android.permission.READ_CONTACTS" /> 

    In main.xml

    Das Android ist ein Google Android Fan-Website, Alles ├╝ber Android Phones, Android Wear, Android Dev und Android Spiele Apps und so weiter.