Android入门:ContentProvider
- - ITeye博客一、ContentProvider介绍. ContentProvider翻译为“内容提供者”;. 定义:指该应用包含一些方法,供外界访问,其他应用程序可以调用该方法,比如如果应用A创建了一个数据库“test.db”,默认是私有的,即其他应用程序不能对其进行操作,但是如果应用A使用了ContentProvider,则其他应用程序可以访问该数据库;.
联系人提供者是一个很强很灵活的应用组件,用来管理联系人信息,可以方便的操作和查询联系人信息!
主要的3张表格,contact,raw contact,data,但是我们操作主要为raw contact,data两张表
raw contact table
data table
/** * 使用批处理,插入联系人信息 * 插入姓名,email,家庭电话,工作电话,手机号码信息 * @param view */ public void insert() { String name = etName.getText().toString().trim(); String email = etEmail.getText().toString().trim(); String numHome = etNumHome.getText().toString().trim(); String numWork = etNumWork.getText().toString().trim(); String numMobile = etNumMobile.getText().toString().trim(); ArrayList<ContentProviderOperation> list = new ArrayList<ContentProviderOperation>(); ContentProviderOperation.Builder builder = null; // 插入原始数据 builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_NAME, "") .withValue(RawContacts.ACCOUNT_TYPE, "") .withValue(RawContacts.DELETED, 0); list.add(builder.build()); // 插入用户名 builder = ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(StructuredName.DISPLAY_NAME, name) .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) .withValueBackReference(Data.RAW_CONTACT_ID, 0); list.add(builder.build()); // 插入email builder = ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Email.ADDRESS, email) .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE) .withValueBackReference(Data.RAW_CONTACT_ID, 0); list.add(builder.build()); // 插入 家庭电话号码 builder = ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Phone.NUMBER, numHome) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.TYPE, Phone.TYPE_HOME) .withValueBackReference(Data.RAW_CONTACT_ID, 0); list.add(builder.build()); // 插入工作电话号码 builder = ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Phone.NUMBER, numWork) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.TYPE, Phone.TYPE_WORK) .withValueBackReference(Data.RAW_CONTACT_ID, 0); list.add(builder.build()); // 插入手机号码 builder = ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Phone.NUMBER, numMobile) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.TYPE, Phone.TYPE_MOBILE) .withValueBackReference(Data.RAW_CONTACT_ID, 0); list.add(builder.build()); try { resolver.applyBatch(ContactsContract.AUTHORITY, list); showOk(); System.out.println("插入成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("插入失败!"); } }
/** * 自定义加载线程,访问通讯录联系人信息 * * @author Administrator * */ private class LoadContactThread extends Thread { @Override public void run() { List<ContactsInfo> infos = new ArrayList<ContactsInfo>(); Cursor cursor = resolver.query(RawContacts.CONTENT_URI, new String[] { RawContacts._ID }, RawContacts.DELETED + "=?", new String[] { "0" }, null); while (cursor.moveToNext()) { ContactsInfo info = new ContactsInfo(); String id = cursor.getString(0); info.setId(id); Cursor c = resolver.query(Data.CONTENT_URI, new String[] { "data1", "data2", "mimetype" }, Data.RAW_CONTACT_ID + "=?", new String[] { id }, null); while (c.moveToNext()) { String data = c.getString(0); String type = c.getString(1); String mimetype = c.getString(2); if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) { // 用户名 info.setName(data); } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) { // email info.setEmail(data); } else if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { // 电话 int t = Integer.parseInt(type); if (Phone.TYPE_HOME == t) { // 家庭电话 info.setNumHome(data); } else if (Phone.TYPE_WORK == t) { // 工作电话 info.setNumWork(data); } else if (Phone.TYPE_MOBILE == t) {// 手机 info.setNumMobile(data); } } } c.close(); // 关闭游标 infos.add(info);// 添加进集合 } cursor.close(); // 关闭游标 Message msg = handler.obtainMessage(); msg.obj = infos; handler.sendMessage(msg); } }