搜索
您的当前位置:首页正文

获取系统通讯录联系人

来源:二三娱乐

效果图:


PickContact.jpg

01 layout: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ccc" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="    *" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发件人" />
        <EditText
            android:id="@id/et_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:hint="请输入姓名" />
        <ImageView
            android:id="@id/iv_contact"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:padding="5dp"
            android:src="@mipmap/contact" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="联系方式" />
        <EditText
            android:id="@id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入手机号码"
            android:inputType="number" />
    </LinearLayout>
</LinearLayout>

02 activity: MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiUI();
    }
    private void intiUI() {
        ImageView iv_contact = (ImageView) findViewById(R.id.iv_contact);
        iv_contact.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.iv_contact:
            // 打开系统通讯录界面
            startActivityForResult(new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI), 0);
            break;
        }
    }

    // 获取联系人界面返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            ContentResolver contentResolver = getContentResolver();
            // 获取联系人
            Uri contactData = data.getData();
            Cursor cursor = contentResolver.query(contactData, null, null, null, null);
            cursor.moveToFirst();
            String username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            // 获取电话号码
            Cursor phoneCursor = contentResolver.query(
                                            
                                            null,
                                             + " = " + contactId,
                                            null, null);
            phoneCursor.moveToFirst();
            String usernumber = 
            usernumber = usernumber.replace(" ", "").replace("-", "");
            phoneCursor.close();
            cursor.close();
            // 设置返回结果
            EditText et_name = (EditText) findViewById(R.id.et_name);
            EditText et_phone = (EditText) findViewById(R.id.et_phone);
            et_name.setText(username);
            et_phone.setText(usernumber);
        }
    }
}

03 在AndroidManifest.xml中添加permission

<uses-permission android:name="android.permission.READ_CONTACTS"/>
Top