如何在Android中添加联系人,如skype,whatsapp在原生联系人应用程序中?

时间:2014-06-16 19:40:54

标签: java android

我正在创建一个联系人应用程序,但想要在我的应用程序中添加联系人,就像Skype或WhatsApp一样。我需要扩展哪个类来实现此功能?

以下是我想要创建的图片:

enter image description here

1 个答案:

答案 0 :(得分:-3)

好的,如果我明白你在找什么。您想使用原生Android联系人列表..如果是,那么以下是步骤:

  1. 结果的火灾意图
  2. 收到查找意图结果代码的结果意图。
  3. 返回带有联系信息的光标
  4. 设置值。
  5. 一个简短的例子。火意图

    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.
            }
        }  
    

    以下是对游标的示例调用。请阅读有关Android文档中联系人光标的更多信息。

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