通讯录的增删改查
(通讯录开发第一天(附)效果图)
> 第一天,把通讯录的业务bean写好,然后写一个dao类,主要用于业务bean的增删改查
效果图这里写图片描述
代码块
通讯录的业务contactbean类,主要有联系人的id,姓名,电话,还有头像,至于其他的属性是在后面做其他模块需要用到后加上了
public class Contactbean {
private Long id;
private String contact_id;
private String name;
private String number;
private Bitmap phone;
private String selectPinyin;
private String formattedNumber;
public String getFormattedNumber() {
return formattedNumber;
}
public void setFormattedNumber(String formattedNumber) {
this.formattedNumber = formattedNumber;
}
public String getSelectPinyin() {
return selectPinyin;
}
public void setSelectPinyin(String selectPinyin) {
this.selectPinyin = selectPinyin;
}
private boolean isCheck;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean isCheck) {
this.isCheck = isCheck;
}
public String getContact_id() {
return contact_id;
}
public void setContact_id(String contact_id) {
this.contact_id = contact_id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Bitmap getPhone() {
return phone;
}
public void setPhone(Bitmap phone) {
this.phone = phone;
}
public void cantactbean(){
}
public void cantactbean(Bitmap bitmap){
this.phone = bitmap;
}
/**
*
* @param id 联系人ID
* @param name 姓名
* @param number 电话
* @param bitmap 头像
*/
public void cntactbean(long id,String name,String number,Bitmap bitmap){
this.id = id;
this.name = name;
this.number = number;
this.phone = bitmap;
}
public void cntactbean(long id,String name,String number,Bitmap bitmap,String selectPinyin){
this.id = id;
this.name = name;
this.number = number;
this.phone = bitmap;
this.selectPinyin=selectPinyin;
}
}
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
/**
* 添加联系人
* @param context上下文
* @param name名字
* @param number号码
*/
public void addContact(Context context, String name, String number) {
ContentValues values = new ContentValues();
Uri rawContactUri = context.getContentResolver().insert(
RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
// 向data表插入数据
if (name != "") {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.GIVEN_NAME, name);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
// 向data表插入电话号码
if (name != "") {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, number);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
}
/**
* 删除联系人
* @param context 上下文
* @param id 联系人的id
*/
public void deleteContact(Context context, long id) {
context.getContentResolver().delete(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, id), null,
null);
}
/**
*
* @param context
* 上下文
* @param rawContactId
* 联系人rawContactId的id
* @param contactbean
* 联系人
*/
public void updataCotact(Context context, long rawContactId,
Contactbean contactbean) {
ContentValues values = new ContentValues();
values.put(Phone.NUMBER, contactbean.getNumber());
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 };
context.getContentResolver().update(ContactsContract.Data.CONTENT_URI,
values, where, selectionArgs);
}
/**
* 查找
* @param context
* @param name
* @return 根据姓名查找对应的号码
*/
public String findContac(Context context, String name) {
String[] p = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };
ContentResolver resolver = context.getContentResolver();
String number = null;
Cursor cursor = resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, p, null,
null, null);
if(cursor!=null){
while (cursor.moveToNext()) {
String phoneName = cursor.getString(cursor
.getColumnIndex(Phone.DISPLAY_NAME));
if(phoneName.equals(name)){
String phoneNumber = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));// 拿到电话号码的列
if (TextUtils.isEmpty(phoneNumber))
continue;
number =phoneNumber;
}
}
}
cursor.close();
return number;
}
/**
* 获取手机全部联系人
*
* @param context
* 上下文
* @return 返回联系人的集合
*/
private static final String TAG = "Contact";
@SuppressLint("InlinedApi")
private static final String[] PHONE_PROJECTION = new String[] {
// 姓名 电话号码 联系人头像ID 联系人id
Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID};
public List<Contactbean> contact(Context context) {
ContentResolver resolver = context.getContentResolver();
List<Contactbean> contactbeans = new ArrayList<Contactbean>();
Contactbean contact = null;
resolver = context.getContentResolver();
Cursor phoneCursor = resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PHONE_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
contact = new Contactbean();
String phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndex(Phone.NUMBER));// 拿到电话号码的列
// 如果电话号码为空,继续
if (TextUtils.isEmpty(phoneNumber))
continue;
contact.setNumber(phoneNumber);
String phoneName = phoneCursor.getString(phoneCursor
.getColumnIndex(Phone.DISPLAY_NAME));
contact.setName(phoneName);
CharacterParser characterParser = CharacterParser.getInstance();
String pinyin = characterParser.getSelling(phoneName);
String sortString = pinyin.substring(0, 1).toUpperCase();
// 正则表达式,判断首字母是否是英文字母
if(sortString.matches("[A-Z]")){
contact.setSelectPinyin(sortString.toUpperCase());
}else{
contact.setSelectPinyin("#");
}
long photoid = phoneCursor.getLong(phoneCursor
.getColumnIndex(Phone.PHOTO_ID));
long contactid = phoneCursor.getLong(phoneCursor
.getColumnIndex(Phone.CONTACT_ID));
Bitmap photo = null;
if (photoid > 0) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactid);
InputStream is = ContactsContract.Contacts
.openContactPhotoInputStream(resolver, uri);
photo = BitmapFactory.decodeStream(is);
}
contact.setId(contactid);
contact.setPhone(photo);
printLog(contact.toString());
contactbeans.add(contact);
}
}
phoneCursor.close();
return contactbeans;
}
>测试上面获取的方法是否正确
public class TextContact extends AndroidTestCase {
public void TextAdd()throws Exception{
Contact contact = new Contact();
contact.addContact(getContext(), "张三","156766666");
}
public void TextUpdata()throws Exception{
Contactbean contactbean = new Contactbean();
contactbean.setNumber("1567555543");
Contact contact = new Contact();
contact.updataCotact(getContext(), 282, contactbean);
}
public void TextDelete()throws Exception{
Contact contact = new Contact();
contact.deleteContact(getContext(), 227);
}
public void Textfind()throws Exception{
Contact contact = new Contact();
String number =contact.findContac(getContext(), "猪婆");
System.out.println(number);
}
private void printLog(String name, String date, String body, String string) {
}
public void TextgetAll()throws Exception{
List<Contactbean> contacts = new ArrayList<Contactbean>();
Contact contact = new Contact();
contacts = contact.contact(getContext());
for(Contactbean info:contacts){
System.out.println(info.getSelectPinyin());
}
}
}
>到这里我们的业务方法也基本写完了,通过内容提供者,以及查询数据库获取到手机联系人的全部信息。