Android 获取通讯录所有的联系人的方法 json打印的就是这些数据

private void readContacts() throws JSONException {
		// 首先,从raw_contacts中读取联系人的id("contact_id")
		// 其次,根据contact_id从data表中查询出相应的电话号码和联系人名称
		// 然后,根据mimetype来区分哪个是联系人,哪个是电话号码
		// Uri rawContactsUri = Uri
		// .parse("content://com.android.contacts/raw_contacts");

		contacts = new ArrayList>();

		// 从raw_contacts中读取联系人的id("contact_id")
		Cursor rawContactsCursor = getContentResolver().query(
				ContactsContract.RawContacts.CONTENT_URI,
				new String[] { "contact_id", "sort_key" }, null, null,
				"sort_key");	//按sort_key排序

		if (rawContactsCursor != null) {
			while (rawContactsCursor.moveToNext()) {
				String contactId = rawContactsCursor.getString(0);
				// System.out.println(contactId);
				if (contactId != null) {
					// 曾经有过,已经删除的联系人在raw_contacts表中记录仍在,但contact_id值为null
					// 根据contact_id从data表中查询出相应的电话号码和联系人名称
					// Uri dataUri = Uri
					// .parse("content://com.android.contacts/data");
					Cursor dataCursor = getContentResolver().query(
							ContactsContract.Data.CONTENT_URI,
							new String[] { "data1", "mimetype" },
							"contact_id=?", new String[] { contactId }, null);

					if (dataCursor != null) {
						HashMap map = new HashMap();

						while (dataCursor.moveToNext()) {
							String data1 = dataCursor.getString(0);
							String mimetype = dataCursor.getString(1);

							if ("vnd.android.cursor.item/phone_v2"
									.equals(mimetype))
								map.put("phone", data1);
							else if ("vnd.android.cursor.item/name"
									.equals(mimetype))
								map.put("name", data1);
						}
						contacts.add(map);
					}
					dataCursor.close();
				}
			}
			Gson gson = new Gson();
			String json=gson.toJson(contacts);
			Log.e("json",json);
			rawContactsCursor.close();
		}
	}



package com.vincentf.contact;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class MainActivity extends Activity {


    private ArrayList> contacts;

    private String data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        getjson();
        readContacts();
    }

    private void readContacts() {
        contacts = new ArrayList>();
        Cursor rawContactsCursor = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[]{"contact_id", "sort_key"}, null, null, "sort_key");    //按sort_key排序
        if (rawContactsCursor != null) {
            while (rawContactsCursor.moveToNext()) {
                String contactId = rawContactsCursor.getString(0);
                // System.out.println(contactId);
                if (contactId != null) {
                    Cursor dataCursor = getContentResolver().query(
                            ContactsContract.Data.CONTENT_URI, new String[]{"data1", "mimetype"},
                            "contact_id=?", new String[]{contactId}, null);
                    if (dataCursor != null) {
                        HashMap map = new HashMap();

                        while (dataCursor.moveToNext()) {
                            String data1 = dataCursor.getString(0);
                            String mimetype = dataCursor.getString(1);
//                            Gson gson = new Gson();
//                           final  String json1=gson.toJson(data1);
                            data = data1;
                            if ("vnd.android.cursor.item/phone_v2".equals(mimetype))
                                map.put("phone", data1);
                            else if ("vnd.android.cursor.item/name".equals(mimetype))
                                map.put("name", data1);
                        }
                        contacts.add(map);
                    }
                    dataCursor.close();
                }
            }

            JSONArray arr = new JSONArray();
            for(Map map : contacts){
                JSONObject obj = new JSONObject(map);
                arr.put(obj);
            }
            JSONObject result = new JSONObject();
            try {
                result.put("contacts", arr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.e("自定义的json",result.toString());
        }


    }

//    private void getjson() {
//        JSONObject jsonObject = new JSONObject();
//        JSONArray jsonArray = new JSONArray();
//        JSONObject object_1 = new JSONObject();
//        JSONObject object_2 = new JSONObject();
//        JSONObject object_3 = new JSONObject();
//        try {
//            object_1.put("id", "3");
//            object_1.put("id1", "1");
//            object_1.put("id2", "2");
//            object_2.put("id3", "3");
//            object_2.put("id4", "4");
//            object_2.put("id5", "5");
//            object_3.put("id6", "6");
//            object_3.put("i7", "7");
//            object_3.put("id8", "9");
//            jsonArray.put(object_1);
//            jsonArray.put(object_2);
//            jsonArray.put(object_3);
//            jsonObject.put("la", jsonArray);
//            jsonObject.put("qita", "5666666");
//        } catch (JSONException e) {
//            e.printStackTrace();
//        } Log.e("自定义的json", jsonObject.toString());
//    }
}



你可能感兴趣的:(安卓类)