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. Android自动判定输入的是电话号码还是网址
  2. android联系人中英文混合排序
  3. android 通过ContentResolver获得联系人数据
  4. android打开联系人的代码
  5. 实现类似Android联系人搜索功能
  6. android获取联系人所有内容
  7. Android 添加删除联系人2.0之前与2.0之后
  8. Android 学习笔记 Contacts (二)Contacts 联系人详解

随机推荐

  1. Android 之 日期选择器
  2. 【Android开发学习06】Android中的文件I/
  3. Android 为模拟器安装其他软件
  4. Android音频流程一(JNI部分)
  5. Android通过调用Webservice实现天气预报
  6. 关于Android studio 使用fastjson报错的
  7. android中padding和margin的区别
  8. ScrollView常用属性汇总
  9. Android平滑移动——Scroller类研究
  10. 声波通信、声波传输原理及源代码