首页 > 代码库 > 获取系统联系人&&添加系统联系人
获取系统联系人&&添加系统联系人
联系人在这个包下
contacts2.db数据库
查询所有的联系人;
需要读写contact权限,注意Uri content是小写的
找这个错误找了一个小时,汗- -!!
这个程序只是添加个按钮。
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();
}
}
结果:
--------------------------------------------------------------------------
(使用该方法不成功,
从网上另外找到一种方法,不管最后一个_id是多少,直接插入一个空的)
也需要读写联系人权限
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();
}
}
来自为知笔记(Wiz)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。