看了下第一期的访问量,100都没有可能很多人不需要吧,之前跟过一个大神学做项目,他都不吧核心代码告诉我,感觉没有一点分享精神,所以我就自己做。没跟他一起做
这里把上次的代码分享一下 我这里分享的跟我博客里面可能不一样因为我已经做完第一个模块了,代码就没有分开了
第一期的代码地址:http://download.csdn.net/detail/u010982856/8232855
分享的是类似的代码 基本跟我的是一样。如果需要的联系人我把 或者加入我的交流群吧
-----------欢迎加入交流群 386451316 有问题一起讨论吧
还废话一下 这期的代码需要在下一期公布地址
开始代码了 布局文件就不写了 这期说得是联系人的问题 在源代码中有相应代码 我也参考了一些
源代码地址:sdk\sources\android-16\com\android\internal\telephony\ 里面是通讯录的所有操作
写一个bean来存放联系人信息(这就一废话是把)
package com.zw.weiyi.enety;
import android.graphics.Bitmap;
public class Person {
private Long id;//联系人id
public String name;//联系人名字
public String phoneNumber;//联系人号码
private Long iconId;//头像id
private Bitmap icon;//头像
private String sortLetters; //显示数据拼音的首字母
public String getSortLetters() {
return sortLetters;
}
public void setSortLetters(String sortLetters) {
this.sortLetters = sortLetters;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getIconId() {
return iconId;
}
public void setIconId(Long iconId) {
this.iconId = iconId;
}
public Bitmap getIcon() {
return icon;
}
public void setIcon(Bitmap icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
这里获取联系人方式有多种 一般很多人会使用AsyncQueryHandler asyncQueryHandler; // 异步查询数据库类对象
这种比较好一点 但我没有用这种 我使用的内容提供者 ContentResolver 来获取 这种更新能力不好
上代码吧 只传了核心代码 我觉得有这些就够了
// 查询条件
public final static String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID };<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//得到联系人的数据
<span style="white-space:pre"> </span>public List<Person> initContacts() {
<span style="white-space:pre"> </span>resolver = context.getContentResolver();
<span style="white-space:pre"> </span>cursor = context.getContentResolver().query(Phone.CONTENT_URI,
<span style="white-space:pre"> </span>PHONES_PROJECTION, null, null, null);
<span style="white-space:pre"> </span>personSize = cursor.getCount();
<span style="white-space:pre"> </span>pAdapter = new personAdapter(context,personsList);
<span style="white-space:pre"> </span>Person person = null;
<span style="white-space:pre"> </span>if (cursor != null) {
<span style="white-space:pre"> </span>for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
<span style="white-space:pre"> </span>.moveToNext()) {
<span style="white-space:pre"> </span>person = new Person();
<span style="white-space:pre"> </span>person.setName(cursor.getString(0));
<span style="white-space:pre"> </span>person.setPhoneNumber(cursor.getString(1));
<span style="white-space:pre"> </span>// 头像id
<span style="white-space:pre"> </span>person.setIconId(cursor.getLong(2));
<span style="white-space:pre"> </span>// 联系人id
<span style="white-space:pre"> </span>person.setId(cursor.getLong(3));
<span style="white-space:pre"> </span>if (cursor.getLong(2) > 0) { // 头像默认为0
<span style="white-space:pre"> </span>Uri uri = ContentUris.withAppendedId(
<span style="white-space:pre"> </span>ContactsContract.Contacts.CONTENT_URI,
<span style="white-space:pre"> </span>person.getId());
<span style="white-space:pre"> </span>InputStream input = ContactsContract.Contacts
<span style="white-space:pre"> </span>.openContactPhotoInputStream(resolver, uri);
<span style="white-space:pre"> </span>person.setIcon(BitmapFactory.decodeStream(input));
<span style="white-space:pre"> </span>} else {// 没有则默认
<span style="white-space:pre"> </span>person.setIcon(BitmapFactory.decodeResource(getResources(),
<span style="white-space:pre"> </span>R.drawable.ic_launcher));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>personsList.add(person);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cursor.close();
<span style="white-space:pre"> </span>return personsList;
<span style="white-space:pre"> </span>}
public void inster(String addnames, String addnames2, String addpnone,
String addohter) throws Exception {
uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = this.getContentResolver();
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
ContentProviderOperation op1 = ContentProviderOperation
.newInsert(uri).withValue("account_name", null).build();
operations.add(op1);
if (!TextUtils.isEmpty(addnames)) {// 为空
uri = Uri.parse("content://com.android.contacts/data");
ContentProviderOperation op2 = ContentProviderOperation
.newInsert(uri)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype", "vnd.android.cursor.item/name")
.withValue("data1", addnames+addnames2)
.build();
operations.add(op2);
}
if (!TextUtils.isEmpty(addpnone)) {
ContentProviderOperation op3 = ContentProviderOperation
.newInsert(uri)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype",
"vnd.android.cursor.item/phone_v2")
.withValue("data1", addpnone).build();
operations.add(op3);
}
// 邮箱
if (!TextUtils.isEmpty(addohter)) {
ContentProviderOperation op4 = ContentProviderOperation
.newInsert(uri)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype",
"vnd.android.cursor.item/email_v2")
.withValue("data1", addohter).build();
operations.add(op4);
}
resolver.applyBatch("com.android.contacts", operations);
Toast.makeText(this, "添加成功", 1000).show();
} // 删除联系人
public void deleteContact(long rawContactId) {
getContentResolver().delete(
ContentUris.withAppendedId(RawContacts.CONTENT_URI,
rawContactId), null, null);
}
// 更新联系人
public void updataCotact(long rawContactId) {
ContentValues values = new ContentValues();
values.put(Phone.NUMBER, "13800138000");
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
String where = ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
+ ContactsContract.Data.MIMETYPE + "=?";
String[] selectionArgs = new String[] { String.valueOf(rawContactId),
Phone.CONTENT_ITEM_TYPE };
getContentResolver().update(ContactsContract.Data.CONTENT_URI, values,
where, selectionArgs);
}
weiyi通讯录(二)获取联系人信息包括头像 增删改查功能,
原文地址:http://blog.csdn.net/u010982856/article/details/41787069