Android中獲取手機中的聯繫人信息


1.0、查看系統通訊錄的表,表路徑:data—>data—>com.android.providers.contacts—>databases—>contacts2.db 
1.1、其中重要的表有:

  1. raw_contacts表(可查看上次通話記錄、可獲取聯繫人的id(“contact_id”));
  2. data表(保存了聯繫人的號碼與名字,可獲取聯繫人的rid(“raw_contact_id”)(id==rid),可獲取信息種類的id(“mimetype_id”));
  3. mimetype表(保存了信息種類名稱,id=5(vnd.android.cursor.item/phone_v2)電話號碼)
  4. 視圖表:view_data表()可查看多表間的數據關係

2.0、流程:

  1. 首先,從raw_contacts中讀取聯繫人的id(“contact_id”)
  2. 其次, 根據contact_id從data表中查詢出相應的電話號碼和聯繫人名稱
  3. 然後,根據mimetype來區分哪個是聯繫人,哪個是電話號碼
  4. 最後將數據填充到ListView中

3.0、佈局: 
1. activity_main.xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yuanfen.myapplication.MainActivity">

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

</android.support.constraint.ConstraintLayout>

2、contact_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp" />
</LinearLayout>

3.1、代碼:

public class MainActivity extends AppCompatActivity {
    private ListView lvList;

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

        lvList = (ListView) findViewById(R.id.lv_list);

        ArrayList<HashMap<String, String>> readContact = readContact();
        // System.out.println(readContact);
        lvList.setAdapter(new SimpleAdapter(this, readContact,
                R.layout.contact_list_item, new String[]{"name", "phone"},
                new int[]{R.id.tv_name, R.id.tv_phone}));
    }

    /**
     * 得到聯繫人
     **/
    private ArrayList<HashMap<String, String>> readContact() {
        // 首先,從raw_contacts中讀取聯繫人的id("contact_id")
        // 其次, 根據contact_id從data表中查詢出相應的電話號碼和聯繫人名稱
        // 然後,根據mimetype來區分哪個是聯繫人,哪個是電話號碼

        Uri rawContactsUri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri dataUri = Uri.parse("content://com.android.contacts/data");

        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        // 從raw_contacts中讀取所有聯繫人的id("contact_id")
        Cursor rawContactsCursor = getContentResolver().query(rawContactsUri,
                new String[]{"contact_id"}, null, null, null);
        if (rawContactsCursor != null) {
            while (rawContactsCursor.moveToNext()) {
                String contactId = rawContactsCursor.getString(0);
                // System.out.println("得到的contact_id="+contactId);

                // 根據contact_id從data表中查詢出相應的電話號碼和聯繫人名稱, 實際上查詢的是視圖view_data
                Cursor dataCursor = getContentResolver().query(dataUri,
                        new String[]{"data1", "mimetype"}, "contact_id=?",
                        new String[]{contactId}, null);

                if (dataCursor != null) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    while (dataCursor.moveToNext()) {
                        String data1 = dataCursor.getString(0);
                        String mimetype = dataCursor.getString(1);
                        // System.out.println(contactId + ";" + data1 + ";"
                        // + mimetype);
                        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);
                        }
                    }
                    list.add(map);
                    dataCursor.close();
                }
            }
            rawContactsCursor.close();
        }
        return list;
    }
}

3.2、添加權限:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

4.0、可調取系統的通訊錄程序得到聯繫人數據


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章