从用户电话簿获取联系人和电子邮件ID

时间:2016-08-30 06:58:46

标签: android android-studio

我想从用户电话簿获取联系人和电子邮件ID,我想在文本字段中填充它们。

我的代码是..

Uri uri = Uri.parse("content://contacts");

 Intent intent = new Intent(Intent.ACTION_PICK, uri);

  intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

  Log.i("please","work");

  startActivityForResult(intent, REQUEST_CODE);

onActivityResult()方法

@Override

protected void onActivityResult(int requestCode, int resultCode,

        Intent intent) {

    if (requestCode == REQUEST_CODE) {

        if (resultCode == RESULT_OK) {

            Uri uri = intent.getData();

            String[] projection = { Phone.NUMBER, Phone.DISPLAY_NAME };

            Cursor cursor = getContentResolver().query(uri, projection,
                    null, null, null);
            cursor.moveToFirst();

            int numberColumnIndex = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(numberColumnIndex);

            int nameColumnIndex = cursor.getColumnIndex(Phone.DISPLAY_NAME);
            String name = cursor.getString(nameColumnIndex);

            Log.d(TAG, "ZZZ number : " + number +" , name : "+name);

        }
    }
};

但这并没有调用onActivityResult函数。

请帮我解决这个问题?

1 个答案:

答案 0 :(得分:0)

获取类似

的联系人列表
 private void insertDummyContact() {


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

            AppController.MyApplication.getInstance().trackScreenView("Contact List Invite");
            Name.add(name);
            Number.add(phoneNumber);


        }
        phones.close();
        //  Log.e("number", Name.toString());
        //  Log.e("name", Number.toString());

        ListAdapter = new ContactListAdapter(ContactListInvite.this, Name, Number);
        ContactList.setAdapter(ListAdapter);
        ListAdapter.notifyDataSetChanged();
    }