package com.android.xiong.getsystemcontentprovidertest;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
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.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button bt1, bt2;
private ExpandableListView exp1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new LookPresonClick());
bt2 = (Button) findViewById(R.id.bt2);
bt2.setOnClickListener(new AddPersonClick());
}
class AddPersonClick implements OnClickListener {
@Override
public void onClick(View v) {
// 获取程序界面中的桑文本框
String name = ((EditText) findViewById(R.id.ed1)).getText()
.toString();
String phone = ((EditText) findViewById(R.id.ed2)).getText()
.toString();
String email = ((EditText) findViewById(R.id.ed3)).getText()
.toString();
// 创建一个空的ContentValue
ContentValues values = new ContentValues();
// 向RawContacts.CONTNT_URI执行一个空值插入
// 目的是获取系统返回的rawContactId
Uri rawContactsUri = getContentResolver().insert(
RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactsUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
// 设置内容类型
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
// 设置联系人名字
values.put(StructuredName.GIVEN_NAME, name);
// 向联系人Uri添加联系人名字
getContentResolver().insert(
android.provider.ContactsContract.Data.CONTENT_URI, values);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
// 设置联系人的电话
values.put(Phone.NUMBER, phone);
// 设置电话类型
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
// 向联系人电话Uri添加电话号码
getContentResolver().insert(
android.provider.ContactsContract.Data.CONTENT_URI, values);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
// 设置联系人的email地址
values.put(Email.DATA, email);
// 设置email的类型
values.put(Email.TYPE, Email.TYPE_WORK);
getContentResolver().insert(
android.provider.ContactsContract.Data.CONTENT_URI, values);
Toast.makeText(MainActivity.this, "添加联系人信息成功", Toast.LENGTH_LONG)
.show();
}
}
class LookPresonClick implements OnClickListener {
@Override
public void onClick(View v) {
// 定义两个List来封装系统联系人信息,指定联系人的电话,email等详情
final ArrayList<String> names = new ArrayList<String>();
final ArrayList<ArrayList<String>> details = new ArrayList<ArrayList<String>>();
// 使用ContentResolver查找联系人数据
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
// 遍历结果 获取系统所有联系人信息
while (cursor.moveToNext()) {
// 获取联系人ID
String contactid = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获取联系人的名字
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
names.add(name);
// 使用ContentResolver查找联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "= ?", new String[] { contactid }, null);
ArrayList<String> detail = new ArrayList<String>();
// 遍历查询结果,获取该联系人的多个电话
while (phones.moveToNext()) {
// 获取查询的结果中的电话号码列
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
detail.add("电话号码是:" + phoneNumber);
}
phones.close();
// 使用ContentResolver查找联系人的E-mail地址
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " =?", new String[] { contactid }, null);
// 遍历查询结果,获取该联系人的多个email地址
while (emails.moveToNext()) {
// 获取查询的结果中email地址中列的数据
String emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
detail.add("email是:" + emailAddress);
}
emails.close();
details.add(detail);
}
cursor.close();
// 加载result.xml界面布局代表的视图
View resultDialog = getLayoutInflater().inflate(R.layout.result,
null);
exp1 = (ExpandableListView) resultDialog.findViewById(R.id.exp1);
// 创建一个ExpandableListAdapter对象
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView text = getTextVeiw();
text.setText(getGroup(groupPosition).toString());
return text;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return names.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return names.get(groupPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return details.get(groupPosition).size();
}
private TextView getTextVeiw() {
AbsListView.LayoutParams lp = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textview = new TextView(MainActivity.this);
textview.setLayoutParams(lp);
textview.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textview.setPadding(36, 0, 0, 0);
textview.setTextSize(20);
return textview;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textview = getTextVeiw();
textview.setText(getChild(groupPosition, childPosition)
.toString());
return textview;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return details.get(groupPosition).get(childPosition);
}
};
exp1.setAdapter(adapter);
// 使用对话框来显示查询结果
new AlertDialog.Builder(MainActivity.this).setView(resultDialog)
.setPositiveButton("确定", null).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入联系人姓名"/>
<EditText
android:id="@+id/ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入联系人电话"/>
<EditText
android:id="@+id/ed3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入联系人email"/>
<Button
android:id="@+id/bt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加联系人信息"/>
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看联系人" />
</LinearLayout>
<?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" >
<ExpandableListView
android:id="@+id/exp1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>