how to access contacts in my android program

后端 未结 4 431
情歌与酒
情歌与酒 2021-01-31 06:58

I\'m making an text message application and want to access contacts in my Android application .

I want to access contacts as if they where in the actual contact list. W

相关标签:
4条回答
  • 2021-01-31 07:03

    check these links:

    How to call Android contacts list?

    How to get contacts from native phonebook in android

    How to obtain all details of a contact in Android

    How to get the first name and last name from Android contacts?

    How to import contacts from phonebook to our application

    Android contacts extraction

    How to get all android contacts but without those which are on SIM

    http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/

    0 讨论(0)
  • 2021-01-31 07:05

    I was able to use @Balaji.K 's example with some modification. The main issue I ran into is that

    while (cursor.moveToNext()) {
    

    will skip over the first result.

    I also wanted to build a list of phone numbers for a user to select from if none are marked as "Mobile". I also pass in a name to search (variable replaced with in this example), but one could modify this code to build a collection by replacing "selection" and "selection_args" with null and iterating over everything in "contacts" as I did for "phones". Here is what I ended up with (in Kotlin):

    val selection = "lower(${ContactsContract.Contacts.DISPLAY_NAME_PRIMARY}) LIKE ?"
    val selectionArgs = arrayOf("<SEARCH_TERM>")
    selectionArgs[0] = "%<SEARCH_TERM>%"
    
    // Query contacts for someone with a matching name
    val contacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
            null, selection, selectionArgs, null)
    contacts!!.moveToFirst()
    if (contacts.count > 1) {
        // TODO: Disambiguate and move to the correct position
    }
    val contactId = contacts.getString(contacts.getColumnIndex(
            ContactsContract.Contacts._ID))
    
    // Check if contact has at least one phone number
    if (contacts.getString(contacts.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER)) == "1") {
    
        val sel = "${ContactsContract.CommonDataKinds.Email.CONTACT_ID} = ?"
        val selArgs = arrayOf(name)
        val sort = "${ContactsContract.CommonDataKinds.Email.LAST_TIME_CONTACTED} DESC"
        selArgs[0] = contactId
    
        val phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, sel, selArgs, sort)
        phones!!.moveToFirst()
    
        // Iterate through phone numbers if multiple
        if (phones.count > 1) {
            val phoneNumbers = mutableMapOf<Int, ContactData>()
            var loop = true
            var mobileIndex: Int? = null
            while (loop) {
                val phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))
                val phoneName = ContactsContract.CommonDataKinds.Phone.getTypeLabel(resources, phoneType, null).toString()
                phoneNumbers[phones.position] = ContactData(phoneName, phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))
                // Remember what position the mobile number was in
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
                    mobileIndex = phones.position
                }
                loop = phones.moveToNext()
            }
            phones.moveToPosition(mobileIndex)
        }
    }
    
    0 讨论(0)
  • 2021-01-31 07:19

    use this code to pick contact:

     Button button = (Button)findViewById(R.id.pickcontact);
    
        button.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                     Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                     startActivityForResult(intent, PICK_CONTACT);
                 }
             });
    
    
    
        @Override public void onActivityResult(int reqCode, int resultCode, Intent data){ super.onActivityResult(reqCode, resultCode, data);
    
        switch(reqCode)
        {
           case (PICK_CONTACT):
             if (resultCode == Activity.RESULT_OK)
             {
                 Uri contactData = data.getData();
                 Cursor c = managedQuery(contactData, null, null, null, null);
              if (c.moveToFirst())
              {
              String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
    
              String hasPhone =
              c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    
              if (hasPhone.equalsIgnoreCase("1")) 
              {
             Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                phones.moveToFirst();
                String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
               // Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();
                setCn(cNumber);
              }
             }
           }
        }
       }
    
    0 讨论(0)
  • 2021-01-31 07:19

    try this code. this stores all the contacts in the ArrayList

    ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
    ContentResolver cr = getContentResolver();
     Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
     while (cursor.moveToNext()) {
         try{
         String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
         String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
         String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
         if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
             Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
             while (phones.moveToNext()) { 
                 String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                 HashMap<String,String> map=new HashMap<String,String>();
                 map.put("name", name);
                 map.put("number", phoneNumber);
                 contactData.add(map);
             } 
             phones.close(); 
         }
     }catch(Exception e){}
     }
    

    if you want to store the specific contact use the if condition.

    0 讨论(0)
提交回复
热议问题