Android读写联系人数据(内容提供者应用)
jopen
11年前
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_getcontacts" android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="read_click" android:layout_weight="1" android:text="读取联系人" /> <Button android:id="@+id/btn_insertcontact" android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="insert_click" android:layout_weight="1" android:text="写入联系人" /> </LinearLayout> </LinearLayout>
核心Activity 两种方式:
package com.example.readcontacts; import java.util.ArrayList; import java.util.List; import com.pas.domain.ContactModel; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.app.Activity; import android.content.ContentProviderOperation; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.view.Menu; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { LinearLayout ll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ll = (LinearLayout) findViewById(R.id.ll); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void read_click(View view) { getContactList(); } public void insert_click(View view) { addContact(); Toast.makeText(this, "添加成功", Toast.LENGTH_LONG).show(); } private void addContact() { ContentResolver resolver = getContentResolver(); ArrayList<ContentProviderOperation> operations=new ArrayList<ContentProviderOperation>(); Uri raw_contacts_uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri data_uri = Uri.parse("content://com.android.contacts/data"); ContentProviderOperation op1=ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME,null) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,null).build(); operations.add(op1); ContentProviderOperation op2=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "王刚") .build(); operations.add(op2); ContentProviderOperation op3=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "990009") .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) .build(); operations.add(op3); ContentProviderOperation op4=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.DATA, "ping@12.com") .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM) .build(); operations.add(op4); try { resolver.applyBatch(ContactsContract.AUTHORITY, operations); } catch (Exception e) { e.printStackTrace(); } // ContentValues raw_values = new ContentValues(); // // Cursor cursor = resolver.query(raw_contacts_uri, new String[] // { "_id" }, null, null, null); // cursor.moveToLast(); // int lastid = cursor.getInt(0); // int newid = lastid + 1; // // raw_values.put("contact_id", newid); // resolver.insert(raw_contacts_uri, raw_values); // // // 电话插入 // ContentValues phonevalues = new ContentValues(); // phonevalues.put("data1", "898989"); // phonevalues.put("mimetype", "vnd.android.cursor.item/phone_v2"); // phonevalues.put("raw_contact_id", newid); // resolver.insert(data_uri, phonevalues); // // // email插入 // ContentValues emailvalues = new ContentValues(); // emailvalues.put("data1", "ping@126.com"); // emailvalues.put("mimetype", "vnd.android.cursor.item/email_v2"); // emailvalues.put("raw_contact_id", newid); // resolver.insert(data_uri, emailvalues); // // // name插入 // ContentValues namevalues = new ContentValues(); // namevalues.put("data1", "王刚"); // namevalues.put("mimetype", "vnd.android.cursor.item/name"); // namevalues.put("raw_contact_id", newid); // resolver.insert(data_uri, namevalues); } private void getContactList() { ContentResolver resolver = getContentResolver(); Uri raw_contacts_uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri data_uri = Uri.parse("content://com.android.contacts/data"); ll.removeAllViews(); Cursor cursor = resolver.query(raw_contacts_uri, null, null, null, null); while (cursor.moveToNext()) { String contactid = cursor.getString(cursor.getColumnIndex("contact_id")); System.out.println(contactid + "\n"); if (null != contactid) { StringBuffer sb = new StringBuffer("联系人ID:"); sb.append(contactid).append(" "); Cursor data_cursor = resolver.query(data_uri, null, "raw_contact_id=?", new String[] { contactid }, null); while (data_cursor.moveToNext()) { String data1 = data_cursor.getString(data_cursor.getColumnIndex("data1")); String mimetype = data_cursor.getString(data_cursor.getColumnIndex("mimetype")); System.out.println("mimetype=" + mimetype + ";data1=" + data1); sb.append(data1).append(" "); } data_cursor.close(); TextView tv = new TextView(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); tv.setLayoutParams(lp); tv.setText(sb.toString()); ll.addView(tv); } } cursor.close(); } }