In Android, how to pick a contact and display it on my app?

前端 未结 6 1743
暖寄归人
暖寄归人 2020-12-21 09:28

I am a beginner to android, i am building a application in which when the user presses a button, the contacts which is stored in the mobile are shown. When he selects a cont

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 09:34

    This gives all the phone numbers from a picked contact in a hassle free manner


    Start the Contact Activity,

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    

    startActivityForResult(intent, PICK_CONTACT);

    Now use the following to get all the data,

    @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);
                 ContentResolver cr = getContentResolver();
    
                 if (c.moveToFirst())
                 {
                     try
                     {
                         String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                         String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                         if (Integer.parseInt(c.getString(c.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);
                            String phone = "";
                            while (pCur.moveToNext()) 
                            {
                                try
                                {
                                    phone = phone + pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts.Data.DATA1)) + ",";
                                }
                                catch(Exception ex)
                                {
    
                                }
                            } 
                            pCur.close();
                            if(phone.length() > 0)
                            {
                                phone = phone.substring(0,phone.length()-1);
                            }
                            txtPhone.setText(phone);
                         }
                         txtName.setText(name); 
                     }
                     catch(Exception ex)
                     {
                         Toast.makeText(AddToInnerCircle.this, "No name selected", Toast.LENGTH_SHORT).show();
                     }
                 }
             }
        }
    }
    

提交回复
热议问题