Android系统一般自带有:电话,邮件,浏览器功能;在电话通讯录及通信记录,均是以ContentProvider的形式向外来应用共享,所以我们要访问和操作通讯录,就需要通过ContentProvider实现。
操作通讯录:逻辑
- 首先需要知道Android系统的通讯录ContentProvider清单类容
- 然后获得其唯一标示
- 再然后去获得通讯录ContentProvider的业务路径
- 最后去操作通讯录的ContentProvider
查看Android自带电话通讯录源代码,得到ContentProvider如下:
查看Android自带电话通讯录源代码,得到通讯录ContentProvider业务路径如下:
查看Android自带电话通讯录源代码,得到通讯录ContentProvider共享表格内容类型如下:
操作类的ContactsService代码:
package com.example.administrator.foundationdemo.contactsprovider.contactsservice;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;
import com.example.administrator.foundationdemo.contactsprovider.domain.ContactText;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/12/20.
*/
public class ContactsService {
private Context context;
public ContactsService(Context context){
this.context = context;
}
//获取所有联系人
public List visitContacts(){
List list = new ArrayList();
Uri uri = Uri.parse("content://com.android.contacts/contacts");//所有联系人的工作路径
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);//获取所有联系人的ID
while (cursor.moveToNext()){
ContactText conract = new ContactText();
int contactcId = cursor.getInt(0);
uri = Uri.parse("content://com.android.contacts/contacts/" + contactcId + "/data");//某一个联系人工作路径
Cursor dateCursor = resolver.query(uri, new String[]{"mimetype","data1","data2"}, null, null, null);//获取某一个联系人的信息
while (dateCursor.moveToNext()){
String date = dateCursor.getString(dateCursor.getColumnIndex("data1"));//信息
String type = dateCursor.getString(dateCursor.getColumnIndex("mimetype"));//内容类型
if("vnd.android.cursor.item/name".equals(type)){//name
conract.setName(date);
}
if("vnd.android.cursor.item/email_v2".equals(type)){//name
conract.setEmail(date);
}
if("vnd.android.cursor.item/phone_v2".equals(type)){//name
conract.setPhone(date);
}
}
dateCursor.close();
list.add(conract);
Log.d("FLY","contactcId"+contactcId);
}
cursor.close();
return list;
}
//根据号码获取联系人的姓名
public String visitContactNameByPhoneNumber(String phoneNumber){
String name = null;
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/"+phoneNumber);//电话查询的工作路径
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri,new String[]{"display_name"},null,null,null);
if (cursor.moveToFirst()){
name = cursor.getString(cursor.getColumnIndex("display_name"));
}else {
name = "未知联系人";
}
cursor.close();
return name;
}
//添加联系人到通讯录
public void addContact(ContactText conract){
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
long contacrId = ContentUris.parseId(resolver.insert(uri,values));
//信息Uri
uri = Uri.parse("content://com.android.contacts/data");
//添加姓名
values.put("raw_contact_id",contacrId);
values.put("mimetype","vnd.android.cursor.item/name");
values.put("data2", conract.getName());
resolver.insert(uri, values);
//添加电话
values.put("raw_contact_id",contacrId);
values.put("mimetype","vnd.android.cursor.item/phone_v2");
values.put("data2", "2");
values.put("data1",conract.getPhone());
resolver.insert(uri,values);
//添加邮箱
values.put("raw_contact_id",contacrId);
values.put("mimetype","vnd.android.cursor.item/Email_v2");
values.put("data2", "2");
values.put("data1",conract.getEmail());
resolver.insert(uri,values);
}
public void addContact2(ContactText conract) throws RemoteException, OperationApplicationException {
ContentResolver resolver = context.getContentResolver();
ArrayList operations= new ArrayList();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentProviderOperation operation_1 = ContentProviderOperation.newInsert(uri)
.withValue("account_name", null)
.build();
operations.add(operation_1);
uri = Uri.parse("content://com.android.contacts/data");
ContentProviderOperation operation_2 = ContentProviderOperation.newInsert(uri)
.withValueBackReference("raw_contact_id",0)//0为list中第一个对象,意思是使用第一个对象操作返回记录ID为这个字段的
.withValue("mimetype","vnd.android.cursor.item/name")
.withValue("data2", conract.getName())
.build();
operations.add(operation_2);
ContentProviderOperation operation_3 = ContentProviderOperation.newInsert(uri)
.withValueBackReference("raw_contact_id", 0)//0为list中第一个对象,意思是使用第一个对象操作返回记录ID为这个字段的
.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
.withValue("data1", conract.getPhone())
.withValue("data2","2" )
.build();
operations.add(operation_3);
ContentProviderOperation operation_4 = ContentProviderOperation.newInsert(uri)
.withValueBackReference("raw_contact_id",0)//0为list中第一个对象,意思是使用第一个对象操作返回记录ID为这个字段的
.withValue("mimetype","vnd.android.cursor.item/Email_v2")
.withValue("data1", conract.getEmail())
.withValue("data2","2" )
.build();
operations.add(operation_4);
resolver.applyBatch("com.android.contacts",operations);
}
}
联系人数据类ContactText代码:
package com.example.administrator.foundationdemo.contactsprovider.domain;
/**
* Created by Administrator on 2016/12/20.
*/
public class ContactText {
private String name;
private String phone;
private String Email;
public ContactText(){
}
public ContactText(String name, String phone, String email) {
this.name = name;
this.phone = phone;
Email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
}
测试类ContactsActivity代码:
package com.example.administrator.foundationdemo.contactsprovider;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.administrator.foundationdemo.R;
import com.example.administrator.foundationdemo.contactsprovider.contactsservice.ContactsService;
import com.example.administrator.foundationdemo.contactsprovider.domain.ContactText;
public class ContactsActivity extends AppCompatActivity {
private EditText contact_name ;
private EditText contact_phone;
private EditText contact_email;
private EditText contact_visit_phone;
private TextView contact_text;
private ContactsService contactsService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
init();
}
private void init(){
contact_name = (EditText) findViewById(R.id.contact_name);
contact_phone = (EditText) findViewById(R.id.contact_phone);
contact_email = (EditText) findViewById(R.id.contact_email);
contact_visit_phone = (EditText) findViewById(R.id.contact_visit_phone);
contact_text = (TextView) findViewById(R.id.contact_text);
contactsService = new ContactsService(this);
}
public void onClick (View view){
ContactText contactText = new ContactText(contact_name.getText().toString(),contact_phone.getText().toString(),contact_email.getText().toString());
switch (view.getId()){
case R.id.add_contact_button1:
contactsService.addContact(contactText);
break;
case R.id.add_contact_button2:
try {
contactsService.addContact2(contactText);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
break;
case R.id.visit_contact_button:
contact_text.setText(contactsService.visitContactNameByPhoneNumber(contact_visit_phone.getText().toString()));
break;
case R.id.visit_contacts_button:
for (ContactText contactText1 :contactsService.visitContacts()){
Log.d("FLY", "contactcId" + contactText1.getName());
contact_text.setText(contact_text.getText().toString()+"\n"+contactText1.getName());
}
break;
}
}
}
Activity界面XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/MatchMatch"
android:orientation="vertical"
tools:context="com.example.administrator.foundationdemo.contactsprovider.ContactsActivity">
<EditText
android:id="@+id/contact_name"
style="@style/MatchWrap"
android:hint="请输入联系人姓名" />
<EditText
android:id="@+id/contact_phone"
style="@style/MatchWrap"
android:inputType="phone"
android:hint="请输入联系人电话" />
<EditText
android:id="@+id/contact_email"
style="@style/MatchWrap"
android:inputType="textEmailAddress"
android:hint="请输入联系人Email" />
<Button
android:id="@+id/add_contact_button1"
android:onClick="onClick"
style="@style/WrapWrap"
android:text="添加联系人1"/>
<Button
android:id="@+id/add_contact_button2"
android:onClick="onClick"
style="@style/WrapWrap"
android:text="添加联系人2"/>
<EditText
android:id="@+id/contact_visit_phone"
style="@style/MatchWrap"
android:inputType="phone"
android:hint="请输入查询电话号码"/>
<Button
android:id="@+id/visit_contact_button"
android:onClick="onClick"
style="@style/WrapWrap"
android:text="查询联系人姓名"/>
<Button
android:id="@+id/visit_contacts_button"
android:onClick="onClick"
style="@style/WrapWrap"
android:text="查询所有"/>
<TextView
android:id="@+id/contact_text"
style="@style/WrapWrap"/>
LinearLayout>
效果图: