获取系统通讯录联系人

作者: RickGe | 来源:发表于2016-11-11 11:10 被阅读363次

    效果图:


    PickContact.jpg

    01 layout: activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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(
                                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                null,
                                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                                                null, null);
                phoneCursor.moveToFirst();
                String usernumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                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"/>
    

    相关文章

      网友评论

        本文标题:获取系统通讯录联系人

        本文链接:https://www.haomeiwen.com/subject/zycfpttx.html