package com.example.getcontacts;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
// 1、查询raw_contactn表获取联系人id
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri datauri = Uri.parse("content://com.android.contacts/data");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("contact_id"));
// System.out.println("id" + id);
if(id!=null){
Cursor datacursor = resolver.query(datauri, null,
"raw_contact_id=?", new String[] { id }, null);
while (datacursor.moveToNext()) {
String data1 = datacursor.getString(datacursor
.getColumnIndex("data1"));
String mimetype = datacursor.getString(datacursor
.getColumnIndex("mimetype"));
System.out.println("data1=" + data1);
System.out.println("mimetype=" + mimetype);
}
datacursor.close();
System.out.println("------------------------");
}
}
cursor.close();
}
}
package com.example.savecontact;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
/*// 1.向raw_contact表添加联系人id
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri datauri = Uri.parse("content://com.android.contacts/data");
ContentResolver resolver = getContentResolver();
// 2.必须知道最后一个联系人id是多少
Cursor cursor=resolver.query(uri, new String[] {"_id"}, null, null, null);
cursor.moveToLast();
int lastId=cursor.getInt(0);
int newId=lastId+1;
ContentValues values=new ContentValues();
values.put("contact_id", newId);
resolver.insert(uri, values);
Log.i(TAG, "看看");
// 3.使用刚才添加的id向data表里添加数据
ContentValues phonevalues=new ContentValues();
phonevalues.put("data1", "1234567");
phonevalues.put("mimetype", "vnd.android.cursor.item/phone_v2");
phonevalues.put("raw_contact_id","newId");
resolver.insert(datauri, phonevalues);
ContentValues emailvalues=new ContentValues();
emailvalues.put("data1", "lisi@qq.com");
emailvalues.put("mimetype", "vnd.android.cursor.item/email_v2");
emailvalues.put("raw_contact_id","newId");
resolver.insert(datauri, emailvalues);
ContentValues namevalues=new ContentValues();
namevalues.put("data1", "lisi");
namevalues.put("mimetype", "vnd.android.cursor.item/name");
namevalues.put("raw_contact_id","newId");
resolver.insert(datauri, namevalues);
*/
//插入raw_contacts表,并获取_id属性
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = getContentResolver();
ContentValues values = new ContentValues();
long contact_id = ContentUris.parseId(resolver.insert(uri, values));
//插入data表
uri = Uri.parse("content://com.android.contacts/data");
//add Name
values.put("raw_contact_id", contact_id);
values.put(Data.MIMETYPE,"vnd.android.cursor.item/name");
values.put("data1", "lisi");
resolver.insert(uri, values);
values.clear();
//add Phone
values.put("raw_contact_id", contact_id);
values.put(Data.MIMETYPE,"vnd.android.cursor.item/phone_v2");
values.put("data1", "99999");
resolver.insert(uri, values);
values.clear();
//add email
values.put("raw_contact_id", contact_id);
values.put(Data.MIMETYPE,"vnd.android.cursor.item/email_v2");
values.put("data1", "abc@qq.com");
resolver.insert(uri, values);
Toast.makeText(this, "添加成功", 0).show();
}
}