自己写的Android Contacts操作类,以后还会补充

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.Contacts;
import android.provider.ContactsContract;

@SuppressWarnings("deprecation")
public class ContactsUtil {
 private static final int REMOVE = 0;
 private static final int ADD = 1;
 Context context;
 public ContactsUtil(Context context){
  this.context = context;
 }
 
 public Boolean addKeepedContacts(long _id){
  int apiLevel = Build.VERSION.SDK_INT;
  if(apiLevel > 7){
   return addOrRemoveKeepedContactsInHighSDK(_id, ContactsUtil.ADD);
  }else{
   return addOrRemoveKeepedContactsInLowSDK(_id, ContactsUtil.ADD);
  }
 }
 
 public Boolean removeKeepedContacts(long _id){
  int apiLevel = Build.VERSION.SDK_INT;
  if(apiLevel > 7){
   return addOrRemoveKeepedContactsInHighSDK(_id, ContactsUtil.REMOVE);
  }else{
   return addOrRemoveKeepedContactsInLowSDK(_id, ContactsUtil.REMOVE);
  }
 }
 
 private Boolean addOrRemoveKeepedContactsInLowSDK(long _id, int flag){
  ContentResolver contentResolver = this.context.getContentResolver();
  Cursor cusor = null;
  String[] projection = new String[] { Contacts.People._ID, Contacts.People.NAME, Contacts.People.NUMBER };
  cusor = contentResolver.query(Contacts.People.CONTENT_URI, projection, Contacts.People._ID + "=?", new String[] { _id + "" }, Contacts.People.NAME + " ASC");
     cusor.moveToFirst();
  ContentValues values = new ContentValues();
  Uri uri = Uri.withAppendedPath(Contacts.People.CONTENT_URI, cusor.getString(cusor.getColumnIndex(Contacts.People._ID)));
//  values.put(Contacts.People.NAME, newName);
  values.put(Contacts.People.STARRED, flag);
//  values.put(Contacts.Phones.NUMBER, newPhoneNum);
  int i = contentResolver.update(uri, values, null, null);
  return i == 1;
 }
 
 private Boolean addOrRemoveKeepedContactsInHighSDK(long _id, int flag){
  ContentValues contentValues = new ContentValues();
  contentValues.put(ContactsContract.Contacts.STARRED, flag);
  int i = this.context.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, contentValues, ContactsContract.Contacts._ID + " = ? ", new String[]{_id + ""});
  return i == 1;
 }
 
 /**
  * 根据ContactID得到电话号码

  * @param _id
  * @return
  */
 public String getPhoneNumbersByContactID(Long _id){
  Cursor cursor = context.getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                null, 
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" 
                + Long.toString(_id), null, null);
  //处理多个号码的情况

  String phoneNumber = "";
  while(cursor.moveToNext()){
   String strNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
   phoneNumber += strNumber + ":";
  }
  cursor.close();
  return phoneNumber;
 }
 
 /**
  *根据电话号码得到ContactsID 

  * @param phoneNumber
  * @return
  */
 public Long getContactIDByPhoneNumber(String phoneNumber){
  Cursor cursor = context.getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                null, 
                ContactsContract.CommonDataKinds.Phone.NUMBER + "=" 
                + phoneNumber, null, null);
  Long ContactID = 0l;
  while(cursor.moveToNext()){
   ContactID = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
  }
  cursor.close();
  return ContactID;
 }
 
 /**
  *根据电话号码,该电话号码加入收藏中 

 * @param phoneNumber
  * @return
  */
 public Boolean addKeepedContactsByPhoneNumber(String phoneNumber){
  //得到对应电话号码的contactID
  Long _id = getContactIDByPhoneNumber(phoneNumber);

 return addKeepedContacts(_id);
 }
 /**
  *根据电话号码,将该电话号码对应的联系人从收藏夹中移除
  * @param phoneNumber
  * @return
  */
 public Boolean removeKeepedContactsByPhoneNumber(String phoneNumber){
   Long _id = getContactIDByPhoneNumber(phoneNumber);
  return removeKeepedContacts(_id);
 }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值