内容提供者访问通讯录

package com.example.fifteen;

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

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/** * @author HD * @date 2015-12-7 * @package_name com.example.fifteen * @file_name MainActivity.java */
public class MainActivity extends Activity {
    private ListView listView;
    List<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lvContact);
        adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        readContacts();
    }

    private void readContacts() {
        // TODO 自动生成的方法存根
        Cursor cursor = null;
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String columnName= ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
        String columnNumber = ContactsContract.CommonDataKinds.Phone.NUMBER; 
        cursor = getContentResolver().query(uri, null, null, null, null);
        if(cursor.moveToNext()){
            do {
                String displayName = cursor.getString(cursor
                        .getColumnIndex(columnName));
                String number = cursor.getString(cursor
                        .getColumnIndex(columnNumber));
                list.add("联系人:" + displayName + "\n" + "电话号码:" + number);
            } while (cursor.moveToNext());
        }
    }


}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.fifteen.MainActivity" >

    <ListView  android:id="@+id/lvContact" android:layout_width="match_parent" android:layout_height="match_parent" >
    </ListView>

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

    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="21" />
    <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>

你可能感兴趣的:(内容提供者访问通讯录)