我无法弄明白......我正在使用Android 2.1 SDK
BUG: *这将转储所有联系人的电话号码 除 以获取CUSTOM LABEL电话号码... *
如何才能转发包含自定义标签的电话号码?
所以,例如,我的一个联系人有3个电话号码...... 2个自定义标签..所以对于该联系人,只有1个电话号码将被转储到日志中。
要运行,只需从任何活动中调用DumpContacts.readContacts(this);
。
package com.abc.debug;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
public class DumpContacts {
private static final String TAG = "Dump Contacts";
static public void readContacts(Context context)
{
String contactId, hasPhone, phoneNumber;
ContentResolver cr=context.getContentResolver();
Cursor phones, cc = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cc.moveToNext())
{
contactId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts._ID));
hasPhone = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
int nameFieldColumnIndex = cc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String contactName = cc.getString(nameFieldColumnIndex);
Log.v(TAG, "Contact id="+contactId+" name="+contactName);
if (Integer.parseInt(hasPhone)==1)
{
// You know it has a number so now query it like this
phones = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
String label=getPhoneLabel(context, phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)),
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));
Log.v(TAG, " Phone"+phoneNumber+" with label="+label);
}
phones.close();
}
}
cc.close();
}
static private String getPhoneLabel(Context context, int type, String label)
{
String s;
switch(type)
{
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
s = "home_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
s = "mobile_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
s = "work_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
s = "fax_work_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
s = "fax_home_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
s = "pager_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
s = "other_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK:
s = "callback_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CAR:
s = "car_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN:
s = "company_main_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN:
s = "isdn_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
s = "main_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX:
s = "other_fax_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO:
s = "radio_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX:
s = "telex_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD:
s = "tty_tdd_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
s = "work_mobile_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER:
s = "work_pager_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT:
s = "assistant_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MMS:
s = "mms_phone";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
if(label == null)
s = "custom";
else
s = "custom:" + label;
break;
default:
s = "default";
}
return s;
}
}
http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html
答案 0 :(得分:1)
试试这个。我已经测试了这段代码。
Uri uri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur=cr.query(uri, null, null, null, sortOrder);
if(cur.getCount()>0){
while(cur.moveToNext()){
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))> 0) {
//get the phone number
Cursor phoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (phoneCur.moveToNext()) {
String phoneNumber= phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phonetype = phoneCur.getInt(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
String phoneLabel = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(this.getResources(), phonetype, customLabel);
Log.e(TAG, "Phone Number: " + phoneNumber + " Selected Phone Label: " + phoneLabel);
}phoneCur.close();
}
}
} cur.close();
答案 1 :(得分:0)
试试这个
static public void readContacts(Context context)
{
String contactId, hasPhone, phoneNumber;
ContentResolver cr=context.getContentResolver();
Cursor phones, cc = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cc.moveToFirst()) {
do
{
contactId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts._ID));
hasPhone = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
int nameFieldColumnIndex = cc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String contactName = cc.getString(nameFieldColumnIndex);
Log.v(TAG, "Contact id="+contactId+" name="+contactName);
if (Integer.parseInt(hasPhone)==1)
{
// You know it has a number so now query it like this
phones = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
String label=getPhoneLabel(context, phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)),
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));
Log.v(TAG, " Phone"+phoneNumber+" with label="+label);
}
phones.close();
}
} while (cc.moveToNext());
}cc.close();
}
答案 2 :(得分:0)
我首先会创建一些辅助类:
/**
* Represents a RawContact (NOTE: This is not the same as a Contact)
* Think of this as an outer join of the raw_contacts table with the data table
*/
public class RawContact {
public String Name;
public int ContactId;
public List<PhoneNumber> Phones;
}
/**
* Represents a high level PhoneNumber from the phone table
*/
public class PhoneNumber {
public String Number;
public int Type;
public String Label;
}
这些允许我们存储我们正在寻找的相关信息。然后,我会使用android.provider.ContactsContract.RawContactsEntity
查找所有原始联系人及其电话号码。注意:一个联系人可以有多个RawContacts,因此进行相应的计划(此示例不考虑这一点)。
package com.contactsample.android;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.RawContactsEntity;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.net.Uri;
import android.util.Log;
import java.util.List;
import java.util.ArrayList;
public class DumpContacts {
public static final String TAG = "DumpContacts";
public static void readContacts(Context context) {
ContentResolver resolver = context.getContentResolver();
String[] projection = new String[] {
RawContactsEntity._ID, // this is the Raw Contacts ID
RawContactsEntity.CONTACT_ID,
RawContactsEntity.DATA_ID, // the data id
Data.MIMETYPE,
Data.DATA1,
Data.DATA2,
Data.DATA3,
Data.DATA4,
Data.DATA5
};
String selection = Data.MIMETYPE + " = ? OR " + Data.MIMETYPE + " = ?";
String[] selectionArgs = new String[] {
Phone.CONTENT_ITEM_TYPE,
StructuredName.CONTENT_ITEM_TYPE
};
Uri uri = RawContactsEntity.CONTENT_URI;
Log.v(TAG, "Fetching from " + uri);
Cursor cursor = resolver.query(uri, projection, selection, selectionArgs, null);
int oldContactId = -1;
RawContact current = null;
List<RawContact> contacts = new ArrayList<RawContact>();
try {
while (cursor.moveToNext()) {
final int rawContactId = cursor.getInt(0);
final int contactId = cursor.getInt(1);
final int dataId = cursor.getInt(2);
final String mimeType = cursor.getString(3);
if (oldContactId != contactId) {
current = new RawContact();
current.ContactId = contactId;
oldContactId = contactId;
contacts.add(current);
}
Log.v(TAG, "ContactID: " + contactId);
if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
current.Name = cursor.getString(4);
Log.v(TAG, "Name: " + current.Name);
} else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
if (current.Phones == null) {
current.Phones = new ArrayList<PhoneNumber>();
}
PhoneNumber phone = new PhoneNumber();
phone.Number = cursor.getString(4);
phone.Type = cursor.getInt(5);
phone.Label = cursor.getString(6);
current.Phones.add(phone);
Log.v(TAG, "Number: " + phone.Number + " Type: " + phone.Type + " Label: " + phone.Label);
}
}
} finally {
cursor.close();
}
Log.v(TAG, "Found " + contacts.size() + " contacts");
}
}
这适用于使用Android 2.1 AVD的模拟器。
它的作用是使每个RawContact的数据行仅包含Phone
和StructuredName
mimetypes(因为这就是我们所关心的)。我们将为每个RawContact(我们关心的)获得至少一行。一行将是StructuredName
或Phone
行,这就是为什么我们在结果循环时检查mimetype以及为什么我们检查联系人ID(以查看我们是否仍在读取数据)目前联系)。
如果RawContact同时包含Phone
和StructuredName
,则每个StructuredName
的{{1}}和n
行都会获得1行RawContact有。