android如何获取手机联系人的数据库

很多人在做手机联系人的apk时会遇到,如何获取手机联系人数据库的问题,在这里我就简单的将代码写一下

package com.example.song.lx_day14_contacts;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

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

/**
 * 获取手机通讯录信息  展示并且修改
 * listview
 * simpleadapter
 * list>
 */
public class MainActivity extends AppCompatActivity {
    private String uri_raw = "content://com.android.contacts/raw_contacts";
    //直接获取data表中的 数据类型是phone
    private String uri_phone = "content://com.android.contacts/data/phones";
    //直接获取data表中数据类型是emali
    private String uri_emalis = "content://com.android.contacts/data/emails";
    private String uri_data = "content://com.android.contacts/data";
    private ListView listView;
    private TextView empty;
    private SimpleAdapter adapter;
    private List> data = new ArrayList<>();
    private ContentResolver contentResolver;//用来访问数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = ((ListView) findViewById(R.id.lv));
        empty = ((TextView) findViewById(R.id.empty));
        listView.setEmptyView(empty);//设置空视图
        adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"_id", "display_Name", "data1"}, new int[]{R.id.id, R.id.name, R.id.number});
        listView.setAdapter(adapter);
        contentResolver = getContentResolver();
        reloadListView();
    }

    private void reloadListView() {
        data.clear();
        data.addAll(selectData());
        adapter.notifyDataSetChanged();

    }

    //查询工作  用于查询data raw_contact标  返回list数据 在刷新adapter
    private List> selectData() {
        /**
         * 1.先查询raw 获取_id查出所有联系人
         * 2.循环查询-->根据_id去data表中(_id=raw_contacts_id)查询 data1数据
         */
        Cursor cursor = contentResolver.query(Uri.parse(uri_raw), new String[]{"_id", "display_name"}, null, null, null);
        //cursor返回的cursor数量不定
        List> list = new ArrayList<>();
        while (cursor != null && cursor.moveToNext()) {
            String _id = cursor.getString(0);
            String displayName = cursor.getString(1);
            Map map = new HashMap<>();
            map.put("_id", _id);
            map.put("display_Name", displayName);
            //根据_id 去data查手机号
            Cursor cursor1 = contentResolver.query(Uri.parse(uri_phone), new String[]{"data1"}, "raw_contact_id=?", new String[]{_id}, null);
            //循环拿到手机号
            StringBuffer sb = new StringBuffer();
            //这里循环拿到手机号是因为有时联系人中  你可能一个人有几个电话
            while (cursor1 != null && cursor1.moveToNext()) {
                sb.append(cursor1.getString(0) + "\n");
            }
            cursor1.close();
            map.put("data1", sb.toString());
            list.add(map);
        }
        cursor.close();
        return list;
    }
}

在这里不要忘记了添加权限


    


你可能感兴趣的:(android)