随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
Android系统提供了Contacts应用程序来管理联系人,而且还为联系人管理提供了ContentProvider。
下面通过一个实例来演示,实现查询系统所有联系人的姓名及其详细信息和添加联系人,代码如下:
读取联系人的Activity:
package com.lovo.activity;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.lovo.lesson15.R;
/**
* 读取联系人的Activity
*
* @author Administrator
*
*/
public class ReadContactActivity extends Activity {
/**
* 保存联系人的姓名集合
*/
private ArrayList<String> namesList = new ArrayList<String>();
/**
* 保存联系人详细信息的集合
*/
private ArrayList<ArrayList<String>> infoList = new ArrayList<ArrayList<String>>();
private ExpandableListView elv;
private Button addContactBtn;
private BaseExpandableListAdapter adapter;
/**
* 初始化集合
*/
public void initData() {
namesList.clear();
infoList.clear();
// 查询联系人的基本信息
// 系统联系人的URI:ContactsContract.Contacts.CONTENT_URI
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
// 获取联系人的ID
int contactId = cursor.getInt(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获取联系人的姓名
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
namesList.add(name);
// 存放每个联系人里面详细信息的集合,里面有电话号码和email
ArrayList detailList = new ArrayList();
// 查找电话号码
// 获取系统联系人电话号码的URI:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
// 电话号码数据中对应联系人的ID列名:ContactsContract.CommonDataKinds.Phone.CONTACT_ID
Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ contactId, null, null);
while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
detailList.add(phoneNumber);
}
phoneCursor.close();
// 查找联系人的email
// 获取联系人email的URI:ContactsContract.CommonDataKinds.Email.CONTENT_URI
// email数据中对应联系人的ID列名:ContactsContract.CommonDataKinds.Email.CONTACT_ID
Cursor emailCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "="
+ contactId, null, null);
while (emailCursor.moveToNext()) {
String email = emailCursor
.getString(emailCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
detailList.add(email);
}
emailCursor.close();
infoList.add(detailList);
}
cursor.close();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_contact_main);
initData();
elv = (ExpandableListView) findViewById(R.id.read_contact_main_elv);
addContactBtn = (Button) findViewById(R.id.read_contact_main_btn_add);
addContactBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ReadContactActivity.this,
SaveContactActivity.class);
startActivity(intent);
}
});
adapter = new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView tv = new TextView(ReadContactActivity.this);
tv.setText(namesList.get(groupPosition));
tv.setPadding(50, 10, 10, 10);
return tv;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return namesList.size();
}
@Override
public Object getGroup(int groupPosition) {
return namesList.get(groupPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
return infoList.get(groupPosition).size();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = new TextView(ReadContactActivity.this);
tv.setText(infoList.get(groupPosition).get(childPosition));
tv.setPadding(10, 10, 10, 10);
return tv;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return infoList.get(groupPosition).get(childPosition);
}
};
elv.setAdapter(adapter);
}
// 添加联系人完成后回调此方法,更新adapter数据
@Override
protected void onResume() {
super.onResume();
initData();
adapter.notifyDataSetChanged();
}
}
读取联系人的布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/read_contact_main_btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" />
<ExpandableListView
android:id="@+id/read_contact_main_elv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>
增加联系人的Activity:
package com.lovo.activity;
import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.view.View;
import android.widget.EditText;
import com.lovo.lesson15.R;
public class SaveContactActivity extends Activity {
private EditText nameText;
private EditText phoneText;
private EditText emailText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_contact_main);
nameText = (EditText) findViewById(R.id.add_contact_main_et_name);
phoneText = (EditText) findViewById(R.id.add_contact_main_et_phone);
emailText = (EditText) findViewById(R.id.add_contact_main_et_email);
}
public void click(View v) {
if (v.getId() == R.id.add_contact_main_btn_ok) {
ContentValues values = new ContentValues();
// 添加一条空数据并获取其ID,此ID为当前增加的联系人ID
Uri uri = getContentResolver().insert(RawContacts.CONTENT_URI,
values);
// 使用工具类ContentUris来解析Uri中的ID
long contactId = ContentUris.parseId(uri);
// 添加联系人的姓名
if (nameText.getText() != null
&& !"".equals(nameText.getText().toString().trim())) {
// 将ContentValues里面的所有内容清空
values.clear();
// 增加联系人ID
values.put(Data.RAW_CONTACT_ID, contactId);
// 指定要添加联系人姓名的MIME类型
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
// 指定要添加联系人的姓名
values.put(StructuredName.GIVEN_NAME, nameText.getText()
.toString());
// 添加联系人的姓名
getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
values);
}
// 添加联系人的电话
if (phoneText.getText() != null
&& !"".equals(phoneText.getText().toString().trim())) {
// 将ContentValues里面的所有内容清空
values.clear();
// 增加联系人ID
values.put(Data.RAW_CONTACT_ID, contactId);
// 指定要添加联系人电话的MIME类型
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
// 指定要添加的联系人电话的类型
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
// 制定要添加联系人的电话
values.put(Phone.NUMBER, phoneText.getText().toString());
// 添加联系人的电话
getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
values);
}
// 添加联系人的email
if (emailText.getText() != null
&& !"".equals(emailText.getText().toString().trim())) {
// 将ContentValues里面的所有内容清空
values.clear();
// 增加联系人ID
values.put(Data.RAW_CONTACT_ID, contactId);
// 指定要添加联系人email的MIME类型
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
// 指定要添加的联系人email的类型
values.put(Email.TYPE, Email.TYPE_HOME);
// 指定要添加的联系人的email
values.put(Email.DATA, emailText.getText().toString());
// 添加联系人的email
getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
values);
}
finish();
}
if (v.getId() == R.id.add_contact_main_btn_back) {
finish();
}
}
}
增加联系人的布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系人的姓名" />
<EditText
android:id="@+id/add_contact_main_et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系人的电话" />
<EditText
android:id="@+id/add_contact_main_et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系人的email" />
<EditText
android:id="@+id/add_contact_main_et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/add_contact_main_btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="保存" />
<Button
android:id="@+id/add_contact_main_btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="click"
android:text="返回" />
</LinearLayout>
</LinearLayout>
要读取系统联系人,不要忘了加上如下权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>