android取得收藏夹的联系人

摘自:http://blog.csdn.net/qiaoning13256/article/details/6889315


/**
* 获得收藏夹的联系人
*/
private void getKeepedContacts(){
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.STARRED + " = 1 " , null, null);
startManagingCursor(cur);
int num = cur.getCount();
System.out.println(num + "");
int count = 0;
while (cur.moveToNext()) {
count ++;

long id = cur.getLong(cur.getColumnIndex("_id"));
Cursor pcur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ Long.toString(id), null, null);

// 处理多个号码的情况
String phoneNumbers = "";
while (pcur.moveToNext()) {
String strPhoneNumber = pcur
.getString(pcur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumbers += strPhoneNumber + ":";
}
phoneNumbers += "\n";
pcur.close();
String name = cur.getString(cur.getColumnIndex("display_name"));
contactNameList.add(name);
contactNumList.add(phoneNumbers);
}
cur.close();
}

/**
* 添加到收藏夹
* @param _id
*/
@SuppressWarnings("deprecation")
private void addKeepedContacts(long _id){
ContentResolver contentResolver = ContactActivity.this.getContentResolver();
Cursor cusor = null;
@SuppressWarnings("deprecation")
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, 1);
// values.put(Contacts.Phones.NUMBER, newPhoneNum);
contentResolver.update(uri, values, null, null);
Toast.makeText(ContactActivity.this, ContactActivity.this.getResources().getString(R.string.add_succeed), Toast.LENGTH_SHORT).show();
}
/**
* 从收藏夹中移出
* @param _id
*/
@SuppressWarnings("deprecation")
private void removeKeepedContacts(long _id){
ContentResolver contentResolver = ContactActivity.this.getContentResolver();
Cursor cusor = null;
@SuppressWarnings("deprecation")
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, 0);
// values.put(Contacts.Phones.NUMBER, newPhoneNum);
contentResolver.update(uri, values, null, null);
new getKeepedContactsTask().execute((Void)null);
Toast.makeText(ContactActivity.this, ContactActivity.this.getResources().getString(R.string.remove_succeed), Toast.LENGTH_SHORT).show();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值