码迷,mamicode.com
首页 > 移动开发 > 详细

安卓之手机通讯录联系人应用

时间:2015-01-12 14:33:38      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

安卓开发关于手机手机联系人:添加联系人,删除联系人,查找联系人等,,在网上找了一些资料,如下:

public class Lianxiren {
Context context;
public Lianxiren(Context context){
this.context = context;
}
/**
* 添加指定联系人
* @param fulName
* @param tel
* @return
*/
protected Boolean addLinkMan(String fulName,String tel){
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
long contactid = ContentUris.parseId(resolver.insert(uri, values));
uri = Uri.parse("content://com.android.contacts/data");
//添加姓名
values.put("raw_contact_id", contactid);
values.put(Data.MIMETYPE, "vnd.android.cursor.item/name");
values.put("data1", fulName);
resolver.insert(uri, values);
values.clear();

//添加电话
values.put("raw_contact_id", contactid);
values.put(Data.MIMETYPE, "vnd.android.cursor.item/phone_v2");
values.put("data1", tel);
resolver.insert(uri, values);
values.clear();
return true;

}
protected String allLinkman(){
StringBuilder sbBuilder = new StringBuilder();
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if(cursor.moveToFirst()){
String contactId =cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(sbBuilder.length() == 0){
sbBuilder.append(name);
}
else{
sbBuilder.append("\n"+name);
}
Cursor phones = ((ContentResolver) cursor).query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "=" + contactId,null,null);
while(phones.moveToNext()){
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//添加Phone的信息
sbBuilder.append("\t").append(phoneNumber);

}
phones.close();

}
while(cursor.moveToNext());
cursor.close();
return sbBuilder.toString();

}
protected Boolean deleteLinkMan (String fulName,String tel) {
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver =context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{Data._ID}, "display_name=?", new String[]{fulName}, null);
if(cursor.moveToFirst()){
int id = cursor.getInt(0);
resolver.delete(uri,"display_name=?" ,new String[]{fulName});
uri = Uri.parse("content://com.android.contacts/data");
resolver.delete(uri, "raw_contact_id=?", new String[]{id + ""});
}

return true;
}
/**
* 查找电话号码,返回联系人姓名
* @throws Exception
*/
public String testContactNameByNumber() throws Exception{
String number = "18381668317";
String name = null ;
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/"+number);
ContentResolver resolver =context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{android.provider.ContactsContract.Data.DISPLAY_NAME}, null, null, null);
if(cursor.moveToFirst()){
name = cursor.getString(0);
Log.i("名字", name);
}
cursor.close();
return name;
}


}

推荐博文:http://www.2cto.com/kf/201409/336307.html

安卓之手机通讯录联系人应用

标签:

原文地址:http://www.cnblogs.com/yuanting/p/4218214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!