【第一个开源项目】最完整的Andoird联系人操作,完美支持联系人的各种属性操作。...

本文介绍了一个Android工程,用于实现联系人的增删改查等功能。通过示例代码展示了如何创建、删除联系人及其相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是android上的一个工程,是从其它地方弄过来的,因为原有的工程有很多 Bug,但是修改起来没慢,也没有申请到权限,我就自己建了一个,我会一直保持这个工程的更新,自己刚好也可以熟悉一下开源的东西怎么玩哈。

地址:http://code.google.com/p/contactlib/

关于工程详细的情况我日后再补。

===================

今日更新:

     更新使用方法:

联系人的操作:

  • Add new Person
                ContactsInterface helper = new ContactsHelper(ctx);
               
PersonContact testGuy = PersonContact.newPersonContact(null);
               
PersonName name = PersonName.newPersonName("FirstName", "LastName",
                               
null, null, null);
               
TelephoneNumber tel = TelephoneNumber.newTelephoneNumber("123123123",
                               
3, null);
               
TelephoneNumber tel2 = TelephoneNumber.newTelephoneNumber("1231231230",
                               
1, null);
               
TelephoneNumber tel3 = TelephoneNumber.newTelephoneNumber(
                               
"123123123123", 2, null);
               
EmailAddress email = EmailAddress.newEmailAddress(
                               
"personcontact@gmail.com", 1, null);
               
PostalAddress postal = PostalAddress.newPostalAddress(1, null,
                               
"test street", null, null, null, null, "519015", null);

               
// PostalAddress postal = PostalAddress.newPostalAddress(1, null,
               
// "test street", "", "", "", "", "519015", "");

               
Organization org = Organization.newOrganization(1, null, null,
                               
"Kingsoft", "Dev", "Android Dev", "10F-B", "Dev_eng", null);
               
Organization org1 = Organization.newOrganization(2, null, null, "Home",
                               
null, null, null, "eater", null);

                testGuy
.name = name;
                testGuy
.addTelephoneNumber(tel);
                testGuy
.addTelephoneNumber(tel2);
                testGuy
.addTelephoneNumber(tel3);
                testGuy
.addEmailAddress(email);
                testGuy
.addOrganization(org);
                testGuy
.addOrganization(org1);
                testGuy
.addPostalAddress(postal);

               
try {
                        helper
.add(testGuy);
               
} catch (RemoteException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
} catch (OperationApplicationException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
}
Delete Person
                ContactsInterface helper = new ContactsHelper(ctx);
               
PersonContact testGuy = PersonContact.newPersonContact(null);
               
PersonName name = PersonName.newPersonName("FirstName1", "LastName1",
                               
null, null, null);
               
TelephoneNumber tel = TelephoneNumber.newTelephoneNumber("1123123123",
                               
3, null);
               
TelephoneNumber tel2 = TelephoneNumber.newTelephoneNumber(
                               
"11231231230", 1, null);
               
TelephoneNumber tel3 = TelephoneNumber.newTelephoneNumber(
                               
"1123123123123", 2, null);
               
EmailAddress email = EmailAddress.newEmailAddress(
                               
"personcontact1@gmail.com", 1, null);
               
PostalAddress postal = PostalAddress.newPostalAddress(1, null,
                               
"1test street", null, null, null, null, "1519015", null);

               
Organization org = Organization.newOrganization(1, null, null,
                               
"Kingsoft", "Dev", "Android Dev", "10F-B", "Dev_eng", null);
               
Organization org1 = Organization.newOrganization(2, null, null, "Home",
                               
null, null, null, "eater", null);

                testGuy
.name = name;
                testGuy
.addTelephoneNumber(tel);
                testGuy
.addTelephoneNumber(tel2);
                testGuy
.addTelephoneNumber(tel3);
                testGuy
.addEmailAddress(email);
                testGuy
.addPostalAddress(postal);

                testGuy
.addOrganization(org);
                testGuy
.addOrganization(org1);
               
try {
                        helper
.add(testGuy);
               
} catch (RemoteException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
} catch (OperationApplicationException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
}

               
PersonContact pc = helper.getPersonContactById(testGuy.id);

               
try {
                        helper
.delete(pc);
               
} catch (RemoteException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
} catch (OperationApplicationException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
}
Delete All Person
                ContactsInterface helper = new ContactsHelper(ctx);

               
Iterator<PersonContact> pcs = helper.listContacts();

               
while (pcs.hasNext()) {
                       
PersonContact p = pcs.next();

                       
try {
                                helper
.delete(p);
                       
} catch (RemoteException e) {
                               
// TODO Auto-generated catch block
                                e
.printStackTrace();
                       
} catch (OperationApplicationException e) {
                               
// TODO Auto-generated catch block
                                e
.printStackTrace();
                       
}
               
}
  • Delete telphone in a Person
                PersonContact pc = helper.getPersonContactById(testGuy.id);
                pc
.fill(ctx);

               
Iterator it = pc.telephoneNumbers.iterator();
               
while (it.hasNext()) {
                       
TelephoneNumber _tel = (TelephoneNumber) it.next();
                       
try {
                                helper
.delete(_tel);
                       
} catch (RemoteException e) {
                               
// TODO Auto-generated catch block
                                e
.printStackTrace();
                       
} catch (OperationApplicationException e) {
                               
// TODO Auto-generated catch block
                                e
.printStackTrace();
                       
}
               
}
update telphone in a Person
                PersonContact pc = helper.getPersonContactById(testGuy.id);
                pc
.fill(ctx);

                pc
.telephoneNumbers.get(0).number = "33333333";

               
try {
                        helper
.update(pc);
               
} catch (RemoteException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
} catch (OperationApplicationException e) {
                       
// TODO Auto-generated catch block
                        e
.printStackTrace();
               
}

其它的操作都类似,详细可查看代码。

转载于:https://www.cnblogs.com/GnagWang/archive/2011/02/07/1949593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值