先定义一些SIM存储联系人的字段:
public class SIMUtils {
public interface SIMColumns {
public final Uri SIM_URI =Uri.parse("content://icc/adn");
public String NAME ="name";
public String TAG ="tag";
public String NEW_TAG ="newTag";
public String NEW_NUMBER ="newNumber";
public String NUMBER ="number";
public String EMAILS ="emails";
public String ID = "_id";
}
public static CursorqueryAllContactsFromSIM(Context context){
returncontext.getContentResolver().query(SIMColumns.SIM_URI, null, null,null, null);
}
//删除联系人
public static boolean deleteContact(Contextcontext,String oldName,String oldNumber){
StringBuffer where = newStringBuffer();
if(!TextUtils.isEmpty(oldName)){
where.append(SIMColumns.TAG+ " = '"+oldName+"'");
}
if(where.toString().length()>0){
//AND一定要大写,否则会出错
where.append("AND ");
}
if(!TextUtils.isEmpty(oldNumber)){
where.append(SIMColumns.NUMBER+ " = '"+oldNumber+"'");
}
returncontext.getContentResolver().delete(SIMColumns.SIM_URI,where.toString(), null)>0;
}
//添加联系人到sim
public static boolean addContact(Contextcontext,String name,String number){
ContentValues values = newContentValues();
values.put(SIMColumns.TAG,name);
values.put(SIMColumns.NUMBER,number);
Uri uri =context.getContentResolver().insert(SIMColumns.SIM_URI,values);
if(uri != null){
returntrue;
}
return false;
}
//更新联系人到sim
public static boolean updateContact(Contextcontext,String oldName,String oldNumber,String newName,StringnewNumber){
ContentValues values = newContentValues();
values.put(SIMColumns.TAG, oldName);
values.put(SIMColumns.NUMBER, oldNumber);
values.put(SIMColumns.NEW_TAG, newName);
values.put(SIMColumns.NEW_NUMBER, newNumber);
return context.getContentResolver().update(SIMColumns.SIM_URI,values, null, null)>0;
}
}