Android开发之contentprovider获取手机所有联系人(姓名、号码、邮箱)

1、得到 ContentResolver 。

ContentResolver resolver = getContentResolver();

2、拿到手机联系人信息(姓名、号码、邮箱)URL。

获取手机联系人:ContactsContract.RawContacts.CONTENT_URI 
获取手机联系人手机号:ContactsContract.CommonDataKinds.Phone.CONTENT_URI 
获取联系人邮箱:ContactsContract.CommonDataKinds.Email.CONTENT_URI

3、根据联系人姓名的id得到相对应联系人的手机号、邮箱等。
//读取联系人的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />

效果如下:

Android开发之contentprovider获取手机所有联系人(姓名、号码、邮箱)_第1张图片

布局activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
xml version="1.0" encoding="utf-8"?>
<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: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="zking.com.g150825_android25_contentprovider02.MainActivity">


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

 

show-list_item.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >

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

    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:text="number" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/number"
        android:text="email" />

RelativeLayout>

MainActivity:

?
1
2
3
4
5
6
7
8
9
10
11
12
package zking.com.g150825_android25_contentprovider02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.RawContacts;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class  MainActivity extends Activity {




    private ListView mLvShow;
    private List> dataList;
    private SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button connection= (Button) findViewById(R.id.connection);

        connection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });


        mLvShow = (ListView) findViewById(R.id.list_item);
        dataList = getDataList();
        adapter = new SimpleAdapter(this, dataList, R.layout.show-list_item//
                , new String[] { "name", "number", "email" }//
                , new int[] { R.id.name, R.id.number, R.id.email });
        mLvShow.setAdapter(adapter);
    }

    
    private List> getDataList() {
        List> list = new ArrayList>();
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(RawContacts.CONTENT_URI,
                new String[] { RawContacts._ID, RawContacts.DISPLAY_NAME_PRIMARY }//
                , null, null, null);
        while (cursor.moveToNext()) {
            Map map = new HashMap();
            String id = cursor.getString(cursor.getColumnIndex(RawContacts._ID));// "_id"
            String name = cursor.getString(cursor.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY));// "display_name"
            map.put("name", name);

            // 联系人的号码
            Cursor phoneCursor = resolver.query(Phone.CONTENT_URI//
                    , new String[] { Phone.NUMBER }// "data1"
                    , "raw_contact_id=?", new String[] { id }, null);
            if (phoneCursor.moveToNext()) {
                String number = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));
                map.put("number", number);
            }

            // 联系人的邮箱
            Cursor emailCursor = resolver.query(Email.CONTENT_URI//
                    , new String[] { Email.ADDRESS}// "data1"
                    , "raw_contact_id=?", new String[] { id }, null);
            if (emailCursor.moveToNext()) {
                String email = emailCursor.getString(emailCursor.getColumnIndex(Email.ADDRESS));
                map.put("email", email);
            }
            list.add(map);
        }
        return list;
    }
}




你可能感兴趣的:(Contentprovider,Android)