使用ContentProvider获取联系人的所有信息

主要是用ContactsContract中的Data数据模型,该数据模型中存储了联系人的所有信息,包括:号码、姓名、Email、邮编、即时通讯账户、个性签名、昵称、个人网站等等,其实Data就是数据库中的一张表而已,该表中有一个比较重要的列叫做:MIMETYPE,有关MIMETYPE与Data的介绍,请看下面的链接,有3篇文章,讲得还可以http://www.cnblogs.com/3dant/archive/2010/12/08/1900634.html
下面就直接贴例子吧

package com.contactscontentprovider;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
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 {

/** Called when the activity is first created. */
@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) {
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, 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();
}
};

b1.setOnClickListener(ocl);
}
}

在获取IM即时通讯信息的时候,我没有找到如何获取通讯类型的方法,但是每一种类型对应一个固定的数值,具体可以看官方文档。如果有哪位大虾知道,请告知,谢谢!!

 

你可能感兴趣的:(Android)