取得 聯絡人, 通話紀錄 資料

1 篇文章 / 0 new
author
取得 聯絡人, 通話紀錄 資料
取得聯絡人資料方式
相關欄位參數 聯絡人使用:ContactsContract.Contacts.xxx , 電話使用:ContactsContract.CommonDataKinds.Phone.xxx
//取得聯絡人
private Cursor getContactsList(String diaplayName) {
    String filter = null;
    String[] whereArgs = null;
    if ((diaplayName != null)&&(diaplayName.trim().length()>0)) {
        filter = ContactsContract.Contacts.DISPLAY_NAME+"=?";
        whereArgs = new String[]{diaplayName};
    }
    Cursor cr = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null,filter,whereArgs, null);
    return cr;
}
//取得電話清單
private Cursor getPhoneList(String contactId) {
    Cursor cr = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?",
            new String[]{ contactId }, null);
    return cr;
}
//取得電話類型
private String getPhoneType(String phone) {
    String phoneType = "";
    //
    Cursor cr = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.NUMBER+" = ?",
                new String[] {phone}, null);
    if (cr.moveToNext()) {
        phoneType = (String)ContactsContract.CommonDataKinds.Phone.getTypeLabel(res,
                cr.getInt(cr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)),
                cr.getString(cr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)) );
    }        
    cr.close();
    //
    return phoneType;
}
//取出所有聯絡人電話
private void getAllContactsPhone() {
    // 取得所有聯絡人                    
    Cursor cr = getContactsList(null);
    while (cr.moveToNext()) {
        Log.d("Contact name", cr.getString(cr.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        if (cr.getInt(cr.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
            // 取得聯絡人所有電話
            Cursor phones = getPhoneList( cr.getString(cr.getColumnIndex(ContactsContract.Contacts._ID)) );
            while (phones.moveToNext()) {
                int phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                String type = (String)ContactsContract.CommonDataKinds.Phone.getTypeLabel(res,phoneType,
                        phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)) );
                Log.d("-Phone", type+":"+phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            }
            phones.close();
        } else {
            Log.d("-Phone", "No Phone");
        }
    };
    cr.close();    
}

取得通話資料方式
相關欄位參數則使用 CallLog.Calls.xxx
//取得通話紀錄
private Cursor getCallLog() { return getCallLog(0); }
private Cursor getCallLog(int callType) {
    String filter = null;
    String[] whereArgs = null;
    switch (callType) {
        case CallLog.Calls.MISSED_TYPE://未接來電
        case CallLog.Calls.INCOMING_TYPE://撥入電話
        case CallLog.Calls.OUTGOING_TYPE://撥出電話
            filter = CallLog.Calls.TYPE+"=?";
            whereArgs = new String[]{ Integer.toString(callType) };
        break;
    }
    Cursor cr = getContentResolver().query(
            CallLog.Calls.CONTENT_URI, null, filter, whereArgs, "date DESC");
    return cr;
}
//清除通話紀錄
private void cleanCallLog() {
    getContentResolver().delete(
            CallLog.Calls.CONTENT_URI, CallLog.Calls.TYPE+"=?",
            new String[] {Integer.toString(CallLog.Calls.MISSED_TYPE)} );
}

 
Free Web Hosting