上文中《Android 获取系统的联系人》主要介绍了怎样获取系统联系人并实战了一下,本文将介绍如何添加一条联系人信息
添加联系人
1. 添加raw_contacts表,添加一条联系人的id
2. 操作data表,插入联系人的数据,每个联系人数据都对应一列 raw_contact_id
新建项目,布局文件代码如下:
<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/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入联系人的姓名" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入联系人的电话" /> <EditText android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入联系人的邮箱" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="save" android:text="提交数据" /> </LinearLayout>
添加相关代码
package com.wuyudong.addcontact; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_name; private EditText et_phone; private EditText et_mail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); et_phone = (EditText) findViewById(R.id.et_phone); et_mail = (EditText) findViewById(R.id.et_email); } public void save(View view) { String name = et_name.getText().toString().trim(); String phone = et_phone.getText().toString().trim(); String email = et_mail.getText().toString().trim(); if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(email)) { Toast.makeText(this, "内容不能为空", 0).show(); } else { // 保存数据 ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, null, null, null, null); int count = cursor.getCount(); ContentValues values = new ContentValues(); int contact_id = count + 1; values.put("contact_id", contact_id); resolver.insert(uri, values); // 2.插入具体的数据到data表,数据类型,data1具体的数据 ContentValues namevalue = new ContentValues(); namevalue.put("mimetype", "vnd.android.cursor.item/name"); // 指定数据类型 namevalue.put("data1", name); namevalue.put("raw_contact_id", contact_id); // 一定要记得指定数据属于哪个联系人 resolver.insert(datauri, namevalue); ContentValues emailvalue = new ContentValues(); emailvalue.put("mimetype", "vnd.android.cursor.item/email_v2"); emailvalue.put("data1", email); emailvalue.put("raw_contact_id", contact_id); resolver.insert(datauri, emailvalue); ContentValues phonevalue = new ContentValues(); phonevalue.put("mimetype", "vnd.android.cursor.item/phone_v2"); phonevalue.put("data1", phone); phonevalue.put("raw_contact_id", contact_id); resolver.insert(datauri, phonevalue); Toast.makeText(this, "添加成功", 0).show(); } } }
时间: 2024-10-23 03:51:43