Android 系统ContentProvider 管理联系人,查询,增加 Name,Phone,E-mail。

时间:2022-09-24 09:34:13

Android 系统ContentProvider 管理联系人,查询,增加 Name,Phone,E-mail。

读写CONTACT需要的权限:

    <uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

activity_content_provider_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="shortcut.song.com.myapplication.ContentProviderTest">


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:onClick="search"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="add"/>


<EditText
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="name"/>

<EditText
android:id="@+id/contact_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="phone"/>

<EditText
android:id="@+id/contact_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"/>

</LinearLayout>

result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<ExpandableListView
android:id="@+id/contact_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

ContentProviderTest.java

package shortcut.song.com.myapplication;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ContentProviderTest extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_provider_test);
}

public void search(View v) {
//定义两个List来封系统的统的联系人信息,指定联系人的电话,Email
final ArrayList<String> names = new ArrayList<>();
final ArrayList<ArrayList<String>> details = new ArrayList<ArrayList<String>>();

// 使用ContentResolver查找联系人信息
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.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 phone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null
,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId , null, null);

ArrayList<String> detail = new ArrayList<>();
// 遍历查询结果,获取该联系人的多个电话号码
while (phone.moveToNext()) {
// 获取查询结果中电话号码列中的数据
String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
detail.add("PHOME:" + phoneNumber);
}
phone.close();

// 使用ContentResolver查找联系人Email
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI
,null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId
,null, null);
// 遍历查询结果,获取该联系人的Email
while(emails.moveToNext()) {
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
detail.add("Email:"+ emailAddress);
}
emails.close();
details.add(detail);

}
cursor.close();

// 加载result.xml 布局
View resultLayout = getLayoutInflater().inflate(R.layout.result, null);
// 获取resultLayout 中ID 为 contact_list 的ExpandableListView
ExpandableListView list = (ExpandableListView)resultLayout.findViewById(R.id.contact_list);
//创建一个ExpandableListAdapter对象
ExpandableListAdapter adapter = new ExpandableListAdapter() {
@Override
public void registerDataSetObserver(DataSetObserver observer) {

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {

}

@Override
public int getGroupCount() {
return names.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return details.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {
return names.get(groupPosition);
}
// 获取指定位置,指定子列表处的子列表数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return details.get(groupPosition).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return true;
}
private TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ContentProviderTest.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 getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());

return textView;
}



// 该方法决定每个子项的外观
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());

return textView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {

return true;
}

@Override
public boolean areAllItemsEnabled() {
return false;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public void onGroupExpanded(int groupPosition) {

}

@Override
public void onGroupCollapsed(int groupPosition) {

}

@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}

@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
};

list.setAdapter(adapter);
new AlertDialog.Builder(ContentProviderTest.this).setView(resultLayout).setPositiveButton("OK", null).show();


}

public void add(View v) {
//获取用户输入
String name = ((EditText)findViewById(R.id.contact_name)).getText().toString();
String phone = ((EditText)findViewById(R.id.contact_phone)).getText().toString();
String email = ((EditText)findViewById(R.id.contact_email)).getText().toString();

//创建ContentValues对象
ContentValues values = new ContentValues();
// 向RawContacts.CONTENT_URI,执行插入一个空值
// 目地是获取系统返回的rawContentId
Uri rawContentUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
long rawContentId = ContentUris.parseId(rawContentUri);
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContentId);
// 设置内容类型
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
// 设置联系人名字
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name);
// 向联系人URI添加联系人名字
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContentId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
// 设置联系人的电话号码。
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone);
// 设置电话类型
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
// 向联系人电话号码URI添加电话号码
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContentId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
// 设置联系人的E-mail地址
values.put(ContactsContract.CommonDataKinds.Email.DATA, email);
// 设置联系人的电子邮件类型
values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
// 向联系人E-mail URI 添加Email数据
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
Toast.makeText(ContentProviderTest.this, "Contact add is ok", Toast.LENGTH_SHORT).show();
}

}

运行效果:

Android 系统ContentProvider 管理联系人,查询,增加 Name,Phone,E-mail。
Android 系统ContentProvider 管理联系人,查询,增加 Name,Phone,E-mail。