Android中读取联系人并显示

<1>.读取联系人有两种方式
(1).去查询系统文件中的表再显示
(2).直接从联系人列表拿值再显示
Android中读取联系人并显示_第1张图片

Android中读取联系人并显示_第2张图片

下面是代码的分享:

MainActivity.java


package com.example.contacts;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity {

    private ContentResolver cr;
    private ListView lv;
    private Cursor cursor;
    private SimpleCursorAdapter simpleCursorAdapter;
    private Uri uri;
    private EditText et_01;
    private EditText et_02;
    private Button bt_01;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_01 = (EditText) this.findViewById(R.id.et_01);
        et_02 = (EditText) this.findViewById(R.id.et_02);
        bt_01 = (Button) this.findViewById(R.id.bt_01);
        bt_01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 0);
            }
        });

        cr = getContentResolver();
        lv = getListView();
        simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_listview,cursor,new String[]{"_id","display_name"},new int[]{R.id.tv_item_listview_mz,R.id.tv_item_listview_id});
        lv.setAdapter(simpleCursorAdapter);
    }
    public void getContacts(View view){
        uri = Uri.parse("content://com.android.contacts/raw_contacts");
        cursor = cr.query(uri,null,null,null,null);
        while (cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name= cursor.getString(cursor.getColumnIndex("display_name"));
            simpleCursorAdapter.changeCursor(cursor);
        }
    }
    public void dx(View view){
        Intent intent=new Intent(this,DXActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                ContentResolver rc = getContentResolver();
                Uri uri = data.getData();
                @SuppressWarnings("deprecation")
                Cursor cursor = managedQuery(uri, null, null, null, null);
                cursor.moveToFirst();
                    // 获取联系人的姓名
                    String name = cursor  .getString(cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    // 获取用户名
                    String contactId = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID));
                    Cursor phone = rc.query(  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID  + " = " + contactId, null, null);
                    while (phone.moveToNext()) {
                        // 获取联系人号码
                        String number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        et_01.setText(number);
                        et_02.setText(name);
                }
            }
        }
    }
}

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.contacts.MainActivity">

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="获取系统联系人"
        android:onClick="getContacts"
        />
    
        
        
        
        
        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt_01"
        android:text="添加联系人"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取短信"
        android:onClick="dx"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_01"
        android:hint="添加的号码"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_02"
        android:hint="添加的名字"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list"
        >ListView>
LinearLayout>

item_listview


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:id="@+id/tv_item_listview_mz"
        android:text="名字"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:id="@+id/tv_item_listview_id"
        android:text="id"
        />


LinearLayout>

最后在AndroidManifest.xml中必须要添加的权限

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

你可能感兴趣的:(Android)