获取系统联系人及电话

package com.example.android.contentprovider;


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


import android.app.ExpandableListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.view.Menu;


public class MainActivity extends ExpandableListActivity {
    private ContentResolver resolver;
    private List grouplist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        grouplist = new ArrayList();
        initData();
        DataAdapter adapter = new DataAdapter(grouplist, this);
        setListAdapter(adapter);
        getExpandableListView().setIndicatorBounds(
                getWindowManager().getDefaultDisplay().getWidth() - 140,
                getWindowManager().getDefaultDisplay().getWidth() - 60);
    }


    private void initData() {
        resolver = getContentResolver();
        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
        Cursor cursor = resolver.query(contactsUri, null, null, null, null);
        while (cursor.moveToNext()) {
            Person person = new Person();
            int contact_id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String contact_name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            person.setId(contact_id);
            person.setName(contact_name);
            List phones = person.getNumber();
            Uri phoneUri = CommonDataKinds.Phone.CONTENT_URI;
            String[] projection = new String[]{"data1"};
            String selection = CommonDataKinds.Phone.CONTACT_ID + " = ?";
            String[] selectionArgs = new String[]{contact_id + ""};
            Cursor cursor_phone = resolver.query(phoneUri, projection, selection, selectionArgs, null);
            while (cursor_phone.moveToNext()) {
                String data = cursor_phone.getString(cursor_phone.getColumnIndex(CommonDataKinds.Phone.NUMBER));
                phones.add(data);

            }

            cursor_phone.close();

// 接下来可以获取邮箱等信息

            grouplist.add(person);

        }
        cursor.close();
    }

}


// person 类

package com.example.android.contentprovider;
import java.util.ArrayList;
import java.util.List;

public class Person {
    private int id;
    private String name;
    private List number;
    private String eml;

    public Person() {
        number = new ArrayList();
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public List getNumber() {
        return number;
    }


    public String getEml() {
        return eml;
    }


    public void setEml(String eml) {
        this.eml = eml;
    }


}

最后记得加上权限:

           
           

 

你可能感兴趣的:(android,联系人)