简体   繁体   中英

How to add contact in Android like skype, whatsapp in native contact app?

I am creating a contact app but want to add contacts in the native android contact app from my app just like Skype or WhatsApp. What class would I need to extend to implement this functionality?

Here is the picture of exactly what I want to create:

在此处输入图片说明

Ok if I understand what you are looking for. You want to use the native Android contact list.. If so then here are the steps:

  1. fire intent for result
  2. receive the intent for result looking for the intent result code.
  3. return a cursor with the contact information
  4. set the values.

A short example. fire intent

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);

//Receive the result
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case CONTACT_PICKER_RESULT:
                //deal with the resulting contact info. I built a separate util class for that.. but here is an example of the code.
                String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,   ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Email.ADDRESS };
                Uri result = data.getData();
                String id = result.getLastPathSegment();
                ContentResolver contentResolver = getActivity().getContentResolver();

                //return cursor
                cur = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
                        ContactsContract.CommonDataKinds.Phone._ID + " like \"" + idUser + "%\"", null, null);
                //Use the cursor to return what you need.
        }
    }  

Here is an example call to the cursor. Please read some more about the contact cursor in the android docs.

email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM