技术开发 频道

PIMItem扩展接口代码分析

  【IT168 技术文档】 PIM,PIMList,PIMItem是pim包的核心基础类,理解了三者之间的关系。那我们就可以随心所欲的使用他们了。

 

PIMItem扩展接口

  PIM现在只支持3种的vCard 格式

  • public static final int CONTACT_LIST = 1;
  • public static final int EVENT_LIST = 2;
  • public static final int TODO_LIST = 3;

 

  在现在pim的版本中,只提供了上面三中格式的vCard,因此pim也只有3个相关的接口扩展,笔者在这里详细的介绍Contact。

  Contact 只是扩展了一些常量。跟一个返回有标记属性的函数:

public int getPreferredIndex(int field);

 

代码一

ontactList contacts = null;

try {

contacts
= (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);

}
catch (PIMException e) {

// An error occurred

return;

}

Contact contact
= contacts.createContact();

String[] addr
= new String[contacts.stringArraySize(Contact.ADDR)];

String[] name
= new String[contacts.stringArraySize(Contact.NAME)];


if (contacts.isSupportedField(Contact.NAME_FORMATTED)

contact.addString(Contact.NAME_FORMATTED, PIMItem.ATTR_NONE,
"Mr. John Q. Public, Esq.");

if (contacts.isSupportedArrayElement(Contact.NAME, Contact.NAME_FAMILY))

name[Contact.NAME_FAMILY]
= "Public";

if (contacts.isSupportedArrayElement(Contact.NAME, Contact.NAME_GIVEN))

name[Contact.NAME_GIVEN]
= "John";

contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);

if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_COUNTRY))

addr[Contact.ADDR_COUNTRY]
= "USA";

if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_LOCALITY))

addr[Contact.ADDR_LOCALITY]
= "Coolsville";

if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_POSTALCODE))

addr[Contact.ADDR_POSTALCODE]
= "91921-1234";

if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_STREET))

addr[Contact.ADDR_STREET]
= "123 Main Street";

if (contacts.isSupportedField(Contact.ADDR))

contact.addStringArray(Contact.ADDR, Contact.ATTR_HOME, addr);

if (contacts.isSupportedField(Contact.TEL))

contact.addString(Contact.TEL, Contact.ATTR_HOME,
"613-123-4567");

if (contacts.maxCategories() != 0

&& contacts.isCategory("Friends"))

contact.addToCategory(
"Friends");

if (contacts.isSupportedField(Contact.BIRTHDAY))

contact.addDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE,
new Date().getTime());

if (contacts.isSupportedField(Contact.EMAIL)) {

contact.addString(Contact.EMAIL, Contact.ATTR_HOME
| Contact.ATTR_PREFERRED, "jqpublic@xyz.dom1.com");

}

try {

contact.commit();

}
catch (PIMException e) {

// An error occured

}

try {

contacts.close();

}
catch (PIMException e) {

}

代码二

ContactList contacts = null;

try {

contacts
= (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);

}
catch (PIMException e) {

// An error occurred

return;

}

Contact contact
= contacts.createContact();


String[] name
= new String[contacts.stringArraySize(Contact.NAME)];

name[Contact.NAME_GIVEN]
= "John";

name[Contact.NAME_FAMILY]
= "Public";


String[] addr
= new String[contacts.stringArraySize(Contact.ADDR)];

addr[Contact.ADDR_COUNTRY]
= "USA";

addr[Contact.ADDR_LOCALITY]
= "Coolsville";

addr[Contact.ADDR_POSTALCODE]
= "91921-1234";

addr[Contact.ADDR_STREET]
= "123 Main Street";


try {

contact.addString(Contact.NAME_FORMATTED, PIMItem.ATTR_NONE,
"Mr. John Q. Public, Esq.");

contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);

contact.addStringArray(Contact.ADDR, Contact.ATTR_HOME, addr);

contact.addString(Contact.TEL, Contact.ATTR_HOME,
"613-123-4567");

contact.addToCategory(
"Friends");

contact.addDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE,
new Date().getTime());

contact.addString(Contact.EMAIL, Contact.ATTR_HOME
| Contact.ATTR_PREFERRED, "jqpublic@xyz.dom1.com");


}
catch (UnsupportedFieldException e) {

// In this case, we choose not to save the contact at all if any of the

// fields are not supported on this platform.

System.out.println(
"Contact not saved");

return;

}


try {

contact.commit();

}
catch (PIMException e) {

// An error occured

}

try {

contacts.close();

}
catch (PIMException e) {

}

ContactList

public interface ContactList extends PIMList {

public abstract Contact createContact();


public abstract Contact importContact(Contact contact);


public abstract void removeContact(Contact contact) throws PIMException;

}

  ContacList扩展PIMList,提供了读取Contact 列表的操作。

 

0
相关文章