需求:写一个应用,有两个按键,分别用来查询所有联系人和添加联系人到系统通讯录。
这里跟上一个练习差不多,基本还是利用系统的provider来做,这里记录一下查看系统provider的Uri的方法,就拿这里这个通讯录的provider来说吧:
1、先将Android源码目录\packages\providers\ContactsProvider目录使用新建一个已存在的安卓工程的方式导入到eclipse中
2、首先去源码中找到 Android源码目录\packages\providers\ContactsProvider\AndroidManifest.xml文件,打开,我们这里想看看联系人的provider是写在哪里的,实际上每个provider都不需要在清单文件中注册,我们只需要在清单文件里浏览
provider android:name="ContactsProvider2"
matcher.addURI(ContactsContract.AUTHORITY, "contacts", CONTACTS);
package com.alexchen.queryandaddcontact;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
private static final String HOST = "content://com.android.contacts/";
private String email;
private String name;
private String address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void printCursor(Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
// 行号
// int position = cursor.getPosition();
int columnCount = cursor.getColumnCount();
// 取出列的名字
for (int i = 0; i < columnCount; i++) {
System.out.print("第" + cursor.getPosition() + "行的" + "第"
+ i + "列名:" + cursor.getColumnName(i) + ";");
System.out.println("值为:" + cursor.getString(i));
}
}
cursor.close();
}
}
/**
* 查询联系人
*
* @param view
*/
public void queryContacts(View view) {
// 1.去raw_contacts表中取出所有联系人的_id
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(HOST + "raw_contacts"),
new String[] { "_id" }, null, null, null);
// printCursor(cursor);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
String selection = "raw_contact_id=?";
String[] selectionArgs = new String[] { String.valueOf(_id) };
// 2.去data表中根据上面取到的_id查询对应id的数据
// 这里resolver要重新获取,因为是不同的cursor
Cursor cursor2 = getContentResolver().query(
Uri.parse(HOST + "data"),
new String[] { "data1", "mimetype" }, selection,
selectionArgs, null);
// printCursor(cursor2);
if (cursor2 != null && cursor2.getCount() > 0) {
while (cursor2.moveToNext()) {
String mimetype = cursor2.getString(1);
// mimetype;值为:vnd.android.cursor.item/phone_v2
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
address = cursor2.getString(0);
System.out.println("address=" + address);
// mimetype;值为:vnd.android.cursor.item/name
} else if ("vnd.android.cursor.item/name"
.equals(mimetype)) {
name = cursor2.getString(0);
System.out.println("name=" + name);
// mimetype;值为:vnd.android.cursor.item/email_v2
} else if ("vnd.android.cursor.item/email_v2"
.equals(mimetype)) {
email = cursor2.getString(0);
System.out.println("email=" + email);
}
}
cursor2.close();
}
}
cursor.close();
}
}
/**
* 插入联系人
*
* @param view
*/
public void addContacts(View view) {
int _id;
// 1.找到raw_contacts表最大的_id
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(HOST + "raw_contacts"),
new String[] { "_id" }, null, null, "_id desc limit 1");
if (cursor != null && cursor.moveToFirst()) {
_id = cursor.getInt(0);
_id += 1;
System.out.println(_id);
// 2.去raw_contacts表中添加新的id
ContentValues values = new ContentValues();
values.put("_id", _id);
getContentResolver().insert(Uri.parse(HOST + "raw_contacts"),
values);
// 3.根据添加的记录的id值,去data表中对应位置添加三条数据记录
// 存号码
values = new ContentValues();
values.put("raw_contact_id", _id);
values.put("mimetype", "vnd.android.cursor.item/phone_v2");
values.put("data1", "10086");
getContentResolver().insert(Uri.parse(HOST + "data"), values);
// 存姓名
values = new ContentValues();
values.put("raw_contact_id", _id);
values.put("mimetype", "vnd.android.cursor.item/name");
values.put("data1", "中国移动");
getContentResolver().insert(Uri.parse(HOST + "raw_contacts"),
values);
values = new ContentValues();
values.put("raw_contact_id", _id);
values.put("mimetype", "vnd.android.cursor.item/email_v2");
values.put("data1", "[email protected]");
getContentResolver().insert(Uri.parse(HOST + "data"), values);
}
}
}