package com.contactscontentprovider;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainContentProvider extends Activity {
private void getContact() {
ContentResolver contentResolver = getContentResolver();
// 获得所有的联系人
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// 循环遍历
if (cursor.moveToFirst()) {
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
// 获得联系人的ID号
String contactId = cursor.getString(idColumn);
// 获得联系人姓名
String disPlayName = cursor.getString(displayNameColumn);
Toast.makeText(MainContentProvider.this, "联系人姓名: "+disPlayName,
Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "联系人姓名: "+disPlayName);
// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (phoneCount > 0) {
// 获得联系人的电话号码列表
Cursor phonesCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phonesCursor.moveToFirst()) {
do {
// 遍历所有的电话号码
String phoneNumber = phonesCursor
.getString(phonesCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(MainContentProvider.this, "联系人电话: "+phoneNumber,
Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "联系人电话: "+phoneNumber);
} while (phonesCursor.moveToNext());
}
phonesCursor.close();
}
//获得联系人的EMAIL
Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID+" = "+contactId, null, null);
if(emailCursor.moveToFirst()){
do{
//遍历所有的email
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));
Toast.makeText(MainContentProvider.this, "Email:"+email, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "Email: "+email);
} while(emailCursor.moveToNext());
}
emailCursor.close();
//获得邮编等信息
Cursor postalCursor = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null,
ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID+" = "+contactId+
" AND "+Data.MIMETYPE+"='"+StructuredPostal.CONTENT_ITEM_TYPE+"'", null, null);
if(postalCursor.moveToFirst()){
do{
String country = postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String city = postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String street = postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String postcode = postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
Toast.makeText(MainContentProvider.this, "国家: "+country+"/n城市: "+city+"/n街道: "+street+"/n邮政编码: "+postcode, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "国家: "+country+"/n城市: "+city+"/n街道: "+street+"/n邮政编码: "+postcode);
}while(postalCursor.moveToNext());
}
postalCursor.close();
//获得IM
Cursor imCursor = getContentResolver().query(Data.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Im.CONTACT_ID+"="+contactId+" AND "+
ContactsContract.Data.MIMETYPE+"='"+ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE+"'", null, null);
if(imCursor.moveToFirst()){
do{
String IM = imCursor.getString(imCursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
int type = imCursor.getInt(imCursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));
switch(type){
case -1: Toast.makeText(MainContentProvider.this, "Custom: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "Custom: "+IM);
break;
case 0: Toast.makeText(MainContentProvider.this, "AIM: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "AIM: "+IM);
break;
case 1: Toast.makeText(MainContentProvider.this, "MSN: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "MSN: "+IM);
break;
case 2: Toast.makeText(MainContentProvider.this, "YAHOO: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "YAHOO: "+IM);
break;
case 3: Toast.makeText(MainContentProvider.this, "SKYPE: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "SKYPE: "+IM);
break;
case 4: Toast.makeText(MainContentProvider.this, "QQ: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "QQ: "+IM);
break;
case 5: Toast.makeText(MainContentProvider.this, "GoogleTalk: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "GoogleTalk: "+IM);
break;
case 6: Toast.makeText(MainContentProvider.this, "ICQ: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "ICQ: "+IM);
break;
case 7: Toast.makeText(MainContentProvider.this, "JABBER: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "JABBER: "+IM);
break;
case 8: Toast.makeText(MainContentProvider.this, "NETMEETING: "+IM, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "NETMEETING: "+IM);
break;
}
}while(imCursor.moveToNext());
}
imCursor.close();
//获取website
Cursor websiteCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Website.URL},
ContactsContract.CommonDataKinds.Website.CONTACT_ID+" = "+contactId +" AND "+
ContactsContract.Data.MIMETYPE+"='"+ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE+"'", null, null);
if(websiteCursor.moveToFirst()){
do{
String website = websiteCursor.getString(websiteCursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
Toast.makeText(MainContentProvider.this, "个人网站: "+website, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "个人网站: "+ website);
}while(websiteCursor.moveToNext());
}
websiteCursor.close();
//获得note
Cursor noteCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Note.NOTE},
ContactsContract.CommonDataKinds.Note.CONTACT_ID+" = "+contactId+" AND "+
ContactsContract.Data.MIMETYPE+"='"+ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE+"'", null, null);
if(noteCursor.moveToFirst()){
String note = noteCursor.getString(noteCursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
Toast.makeText(MainContentProvider.this, "个性签名: "+note, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "个性签名: "+note);
}
noteCursor.close();
//获得nickname
Cursor nicknameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Nickname.NAME},
ContactsContract.CommonDataKinds.Nickname.CONTACT_ID+"="+contactId+" AND "+
ContactsContract.Data.MIMETYPE+"='"+ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE+"'", null, null);
if(nicknameCursor.moveToFirst()){
String nickname = nicknameCursor.getString(nicknameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
Toast.makeText(MainContentProvider.this, "昵称: "+nickname, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "昵称: "+nickname);
}
nicknameCursor.close();
//获得organization
Cursor orgCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Organization.COMPANY,ContactsContract.CommonDataKinds.Organization.TITLE},
ContactsContract.CommonDataKinds.Nickname.CONTACT_ID+"="+contactId+" AND "+
ContactsContract.Data.MIMETYPE+"='"+ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE+"'", null, null);
if(orgCursor.moveToFirst()){
do{
String company = orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
String position = orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
Toast.makeText(MainContentProvider.this, "公司: "+company, Toast.LENGTH_LONG).show();
Log.i("ContentProvider", "公司: "+company+"/n职位: "+position);
}while(orgCursor.moveToNext());
}
orgCursor.close();
} while (cursor.moveToNext());
}
cursor.close();
}
//删除联系人
private void delContact(MainContentProvider mainContentProvider,
String name) {
Cursor cursor = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.RAW_CONTACT_ID}, ContactsContract.Contacts.DISPLAY_NAME+"=?",
new String[]{name}, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
if(cursor.moveToFirst()){
do{
long Id = cursor.getLong(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
ops.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(RawContacts.CONTENT_URI,Id))
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}
}while(cursor.moveToNext());
cursor.close();
}
}
//更新联系人信息
private void updateContact(MainContentProvider mainContentProvider,
String oldname, String name, String phone, String email,
String website, String organization, String note) {
Cursor cursor = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.RAW_CONTACT_ID}, ContactsContract.Contacts.DISPLAY_NAME+"=?",
new String[]{oldname},null);
cursor.moveToFirst();
String id = cursor.getString(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
cursor.close();
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
//更新电话号码
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + ContactsContract.Data.MIMETYPE + " = ?" +
" AND "+Phone.TYPE+"=?",
new String[]{String.valueOf(id),Phone.CONTENT_ITEM_TYPE,String.valueOf(Phone.TYPE_HOME)})
.withValue(Phone.NUMBER, phone)
.build());
//更新email
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + ContactsContract.Data.MIMETYPE + " = ?" +
" AND "+Email.TYPE+"=?",
new String[]{String.valueOf(id),Email.CONTENT_ITEM_TYPE,String.valueOf(Email.TYPE_WORK)})
.withValue(Email.DATA, email)
.build());
//更新姓名
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[]{String.valueOf(id),StructuredName.CONTENT_ITEM_TYPE})
.withValue(StructuredName.DISPLAY_NAME, name)
.build());
//更新网站
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[]{String.valueOf(id),Website.CONTENT_ITEM_TYPE})
.withValue(Website.URL, website)
.build());
//更新公司
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[]{String.valueOf(id),Organization.CONTENT_ITEM_TYPE})
.withValue(Organization.COMPANY, organization)
.build());
//更新note
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[]{String.valueOf(id),Note.CONTENT_ITEM_TYPE})
.withValue(Note.NOTE, note)
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}
}
private void addContact(Context context, String name, String organisation,
String phone, String fax, String email, String address,
String website) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED)
.build());
// add name
if (!name.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
}
// add company
if (!organisation.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,
organisation)
.withValue(ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.build());
}
// add phone
if (!phone.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
.build());
}
// add Fax
if (!fax.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, fax)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK)
.build());
}
// add email
if (!email.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, 1)
.build());
}
// add address
if (!address.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, address)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.build());
}
// add website
if (!website.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Website.URL, website)
.withValue(ContactsContract.CommonDataKinds.Website.TYPE,
ContactsContract.CommonDataKinds.Website.TYPE_WORK)
.build());
}
// add logo image
// Bitmap bm = logo;
// if (bm != null) {
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
// byte[] photo = baos.toByteArray();
// if (photo != null) {
//
// ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
// .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
// .withValue(ContactsContract.Data.MIMETYPE,
// ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
// .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photo)
// .build());
//
// }
// }
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.Button01);
OnClickListener ocl = new OnClickListener() {
public void onClick(View v) {
//添加联系人
addContact(MainContentProvider.this, "小明", "XX公司",
"121236547", "123456", "123@qq.com", "XXXXXX", "www.baidu.com");
//更新联系人信息
updateContact(MainContentProvider.this, "小李", "小花", "1234122445", "541394@qq.com",
"www.baidu.com", "腾讯", "Impossable is nothing!!");
//删除联系人
delContact(MainContentProvider.this, "小白");
//获取联系人所有信息
getContact();
}
};
b1.setOnClickListener(ocl);
}
}