关于对android通讯录有关操作 本人正为了更加了解数据库,正在研究通讯录,但是遇到困难。就是不懂怎么将通讯录的数据取出来做稍微的改变之后又存回去,有哪位大虾能帮帮忙呢,就是取出数据,然后修改一下再放回去,谢谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个肯定是可以做到的,如果是添加联系人可以按照如下代码:public boolean insertContact(Contact contact) { try { ContentValues values = new ContentValues(); Uri rawContactUri = this.cr.insert(RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); String name = contact.getDisplayName(); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); values.put(StructuredName.GIVEN_NAME, name); this.cr.insert(android.provider.ContactsContract.Data.CONTENT_URI, values); for (int i = 0; i < contact.getPhone().size(); i++) { int numType1 = Integer.parseInt(contact.getPhone().get(i) .getType()); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put( Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); values.put( android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getPhone().get(i).getNumber()); values.put( android.provider.ContactsContract.CommonDataKinds.Phone.TYPE, numType1); this.cr.insert( android.provider.ContactsContract.Data.CONTENT_URI, values); } for (int i = 0; i < contact.getEmail().size(); i++) { int emailType = Integer.parseInt(contact.getEmail().get(i) .getType()); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put( Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); values.put( android.provider.ContactsContract.CommonDataKinds.Email.DATA, contact.getEmail().get(i).getAddress()); values.put( android.provider.ContactsContract.CommonDataKinds.Email.TYPE, emailType); this.cr.insert( android.provider.ContactsContract.Data.CONTENT_URI, values); } values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put( Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE); /* * values.put( * android.provider.ContactsContract.CommonDataKinds.Organization * .DATA, contact.getEmail().get(i).getAddress()); */ if (contact.getOrganization().getOrganization() != null && contact.getOrganization().getOrganization() != "") { values.put( android.provider.ContactsContract.CommonDataKinds.Organization.COMPANY, contact.getOrganization().getOrganization()); } if (contact.getOrganization().getTitle() != null && contact.getOrganization().getTitle() != "") { values.put( android.provider.ContactsContract.CommonDataKinds.Organization.TITLE, contact.getOrganization().getTitle()); } this.cr.insert(android.provider.ContactsContract.Data.CONTENT_URI, values); for (int i = 0; i < contact.getAddresses().size(); i++) { int addressType = Integer.parseInt(contact.getAddresses() .get(i).getType()); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put( Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE); values.put( android.provider.ContactsContract.CommonDataKinds.StructuredPostal.STREET, contact.getAddresses().get(i).getStreet()); values.put( android.provider.ContactsContract.CommonDataKinds.StructuredPostal.TYPE, addressType); this.cr.insert( android.provider.ContactsContract.Data.CONTENT_URI, values); } return true; } catch (Exception e) { String strInfo = ""; strInfo += e.toString(); return false; } }如果是修改的话也差不多 就是要先获取raw_contacts表的_id了 根据id来insert 直接用我的程序说明:public void querydata() { ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String rawContactsId = ""; String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); String dispaly_name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // 读取rawContactsId Cursor rawContactsIdCur = cr.query(RawContacts.CONTENT_URI, null, RawContacts.CONTACT_ID +" = ?", new String[]{id}, null); if (rawContactsIdCur.moveToFirst()) { rawContactsId = rawContactsIdCur.getString(rawContactsIdCur.getColumnIndex(RawContacts._ID)); } rawContactsIdCur.close(); // 读取号码 if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor PhoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID +" = ?", new String[]{rawContactsId}, null); int tell = 0; String[] phones = new String[21]; while (PhoneCur.moveToNext()) { String number = PhoneCur.getString(PhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String numberType = PhoneCur.getString(PhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); update(rawContactsId,number); } PhoneCur.close(); }}public void update(String rawRawContactId, String NewNumber) { ContentValues values = new ContentValues(); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, NewNumber); values.put(Phone.TYPE, Phone.TYPE_HOME); String Where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] WhereParams = new String[]{rawRawContactId,Phone.CONTENT_ITEM_TYPE}; getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, Where, WhereParams); } 如何使用自己的aidl android手机病毒特征码 Eclipse和MyEclipse用哪个??? 页面跳转---急 updateViews.setTextViewText问题请教 请问一下一个赋值控件的小问题 sipdroid imsdroid 关系 怎么样用rtmp实现android视频聊天功能,或者其它不错的协议也可 视频播放完成后如何跳转到新的activity 安卓开发不能从values/strings.xml读取信息 关闭最近执行的APP的系统界面的实现 android有没有和symbian的PC Connectivity SDK类似的东西?
ContentValues values = new ContentValues(); Uri rawContactUri = this.cr.insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri); String name = contact.getDisplayName();
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.GIVEN_NAME, name);
this.cr.insert(android.provider.ContactsContract.Data.CONTENT_URI,
values);
for (int i = 0; i < contact.getPhone().size(); i++) {
int numType1 = Integer.parseInt(contact.getPhone().get(i)
.getType());
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(
Data.MIMETYPE,
android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(
android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER,
contact.getPhone().get(i).getNumber());
values.put(
android.provider.ContactsContract.CommonDataKinds.Phone.TYPE,
numType1);
this.cr.insert(
android.provider.ContactsContract.Data.CONTENT_URI,
values);
} for (int i = 0; i < contact.getEmail().size(); i++) {
int emailType = Integer.parseInt(contact.getEmail().get(i)
.getType());
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(
Data.MIMETYPE,
android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
values.put(
android.provider.ContactsContract.CommonDataKinds.Email.DATA,
contact.getEmail().get(i).getAddress());
values.put(
android.provider.ContactsContract.CommonDataKinds.Email.TYPE,
emailType);
this.cr.insert(
android.provider.ContactsContract.Data.CONTENT_URI,
values);
} values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(
Data.MIMETYPE,
android.provider.ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
/*
* values.put(
* android.provider.ContactsContract.CommonDataKinds.Organization
* .DATA, contact.getEmail().get(i).getAddress());
*/
if (contact.getOrganization().getOrganization() != null
&& contact.getOrganization().getOrganization() != "") {
values.put(
android.provider.ContactsContract.CommonDataKinds.Organization.COMPANY,
contact.getOrganization().getOrganization());
} if (contact.getOrganization().getTitle() != null
&& contact.getOrganization().getTitle() != "") {
values.put(
android.provider.ContactsContract.CommonDataKinds.Organization.TITLE,
contact.getOrganization().getTitle());
}
this.cr.insert(android.provider.ContactsContract.Data.CONTENT_URI,
values); for (int i = 0; i < contact.getAddresses().size(); i++) {
int addressType = Integer.parseInt(contact.getAddresses()
.get(i).getType());
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(
Data.MIMETYPE,
android.provider.ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
values.put(
android.provider.ContactsContract.CommonDataKinds.StructuredPostal.STREET,
contact.getAddresses().get(i).getStreet()); values.put(
android.provider.ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
addressType); this.cr.insert(
android.provider.ContactsContract.Data.CONTENT_URI,
values);
} return true;
} catch (Exception e) {
String strInfo = "";
strInfo += e.toString(); return false;
}
}
如果是修改的话也差不多 就是要先获取raw_contacts表的_id了 根据id来insert
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String rawContactsId = "";
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String dispaly_name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// 读取rawContactsId
Cursor rawContactsIdCur = cr.query(RawContacts.CONTENT_URI,
null,
RawContacts.CONTACT_ID +" = ?",
new String[]{id}, null);
if (rawContactsIdCur.moveToFirst())
{
rawContactsId = rawContactsIdCur.getString(rawContactsIdCur.getColumnIndex(RawContacts._ID));
}
rawContactsIdCur.close();
// 读取号码
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor PhoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID +" = ?",
new String[]{rawContactsId}, null);
int tell = 0;
String[] phones = new String[21];
while (PhoneCur.moveToNext())
{
String number = PhoneCur.getString(PhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String numberType = PhoneCur.getString(PhoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); update(rawContactsId,number); }
PhoneCur.close();
}
}public void update(String rawRawContactId, String NewNumber)
{
ContentValues values = new ContentValues();
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, NewNumber);
values.put(Phone.TYPE, Phone.TYPE_HOME);
String Where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] WhereParams = new String[]{rawRawContactId,Phone.CONTENT_ITEM_TYPE};
getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, Where, WhereParams);
}