ContentProvider访问通讯录

MainActivity.java

package com.example.hd.contentpro_contact;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button mButton;
    private StringBuilder builder;
    private ArrayAdapter<String> adapter;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.button1);
        mTextView = (TextView) findViewById(R.id.textview);
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                getContact();
                mTextView.setText(builder.toString());

            }
        });
    }

    public void getContact() {
        builder = new StringBuilder();
        ContentResolver resolver = this.getContentResolver();
        Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        while (cursor.moveToNext()) {
            int nameIndex = cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            String name = cursor.getString(nameIndex);
            builder.append("name = " + name + ";");
            int idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            String id = cursor.getString(idIndex);
            Cursor phoneName = resolver.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
                            + id, null, null);
            while (phoneName.moveToNext()) {
                int Int_phone = phoneName
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String str_Number = phoneName.getString(Int_phone);
                builder.append("number =" + str_Number);
            }
            phoneName.close();
        }
        cursor.close();
    }

}

activity_layout.xml

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

   <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content"/>

    <Button  android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示" />


</LinearLayout>

AndroidMainfest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hd.contentpro_contact" android:versionCode="1" android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="17" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

你可能感兴趣的:(ContentProvider访问通讯录)