Android学习之通过content provider获得联系人

package cn.mycontent;import android.app.Activity;import android.content.ContentResolver;import android.database.Cursor;import android.graphics.Color;import android.net.Uri;import android.os.Bundle;import android.provider.BaseColumns;import android.provider.ContactsContract;import android.widget.TextView;public class Activity01 extends Activity {//查询Content Provider时希望返回的列String [] columns = {ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID,            //People._ID,//People.NAME};Uri contactUri = ContactsContract.Contacts.CONTENT_URI;TextView tv;//Uri contaUri = Contacts.People.CONTENT_URI;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tv = (TextView)findViewById(R.id.tv);        String result = getQueryData();        tv.setTextColor(Color.GREEN);        tv.setTextSize(20.0f);        tv.setText("ID\t名字\n" + result);            }    //获取联系人列表的信息,返回 String对象    public String getQueryData(){    String result = "";    //获取ContentResolver对象    ContentResolver resolver = getContentResolver();    Cursor cursor = resolver.query(contactUri, columns, null, null, null);    //获得_ID字段的索引    int idIndex = cursor.getColumnIndex(BaseColumns._ID);    //获得Name字段的索引    int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);    //遍历Cursor提取数据    for (cursor.moveToFirst();(!cursor.isAfterLast());cursor.moveToNext()) {result = result + cursor.getString(idIndex) + "\t";result = result + cursor.getString(nameIndex)+ "\t\n";}    cursor.close();    return result;    }}

需加上<uses-permission android:name="android.permission.READ_CONTACTS" />权限

附件为自定义content provider