标签:
1 package com.example.visit_contacts; 2 3 import android.app.Activity; 4 import android.content.ContentResolver; 5 import android.content.ContentValues; 6 import android.database.Cursor; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.Menu; 11 import android.view.MenuItem; 12 import android.view.View; 13 14 public class MainActivity extends Activity { 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 } 21 public void getContact(View v){ 22 ContentResolver cr = getContentResolver(); 23 Cursor c = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null); 24 while(c.moveToNext()){ 25 int id = c.getInt(0); 26 Cursor c2 = cr.query(Uri.parse("content://com.android.contacts/data"), new String[]{"mimetype","data1"}, 27 "raw_contact_id=?", new String[]{id+""}, null); 28 Contact contact = new Contact(); 29 while(c2.moveToNext()){ 30 String type = c2.getString(0); 31 String data1 = c2.getString(1); 32 if("vnd.android.cursor.item/name".equals(type)){ 33 contact.setName(data1); 34 } 35 if("vnd.android.cursor.item/phone_v2".equals(type)){ 36 contact.setNumber(data1); 37 } 38 if("vnd.android.cursor.item/email_v2".equals(type)){ 39 contact.setEmail(data1); 40 } 41 Log.i("getContact", contact.toString()); 42 } 43 } 44 } 45 46 public void addContact(View v){ 47 ContentResolver cr = getContentResolver(); 48 //step1:handle raw_contact table; 49 Cursor c = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"_id"}, null, null, null); 50 c.moveToLast(); 51 int id = c.getInt(0); 52 int insert_id = id + 1; 53 54 ContentValues cv = new ContentValues(); 55 cv.put("_id", insert_id); 56 cv.put("contact_id", insert_id); 57 cr.insert(Uri.parse("content://com.android.contacts/raw_contacts"), cv); 58 59 //step2. handle data table 60 //insert name 61 cv.clear(); 62 cv.put("_id", 9); 63 cv.put("mimetype", "vnd.android.cursor.item/name"); 64 cv.put("raw_contact_id", insert_id); 65 cv.put("data1", "zhangsanfeng"); 66 cr.insert(Uri.parse("content://com.android.contacts/data"), cv); 67 68 //insert phone_v2 69 cv.clear(); 70 cv.put("_id", 10); 71 cv.put("mimetype", "vnd.android.cursor.item/phone_v2"); 72 cv.put("raw_contact_id", insert_id); 73 cv.put("data1", "13899998888"); 74 cr.insert(Uri.parse("content://com.android.contacts/data"), cv); 75 76 //insert email 77 cv.clear(); 78 cv.put("_id", 11); 79 cv.put("mimetype", "vnd.android.cursor.item/email_v2"); 80 cv.put("raw_contact_id", insert_id); 81 cv.put("data1", "zsf@163.com"); 82 cr.insert(Uri.parse("content://com.android.contacts/data"), cv); 83 } 84 85 86 }
1 package com.example.visit_contacts; 2 3 public class Contact { 4 5 private String name; 6 private String number; 7 private String email; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getNumber() { 15 return number; 16 } 17 public void setNumber(String number) { 18 this.number = number; 19 } 20 public String getEmail() { 21 return email; 22 } 23 public void setEmail(String email) { 24 this.email = email; 25 } 26 public Contact(String name, String number, String email) { 27 super(); 28 this.name = name; 29 this.number = number; 30 this.email = email; 31 } 32 public Contact() { 33 super(); 34 // TODO Auto-generated constructor stub 35 } 36 @Override 37 public String toString() { 38 return "Contact [name=" + name + ", number=" + number + ", email=" 39 + email + "]"; 40 } 41 42 43 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.visit_contacts.MainActivity" 10 android:orientation="vertical" > 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello_world" /> 16 17 <Button 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:onClick="getContact" 21 android:text="获取系统的联系人信息" /> 22 <Button 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="新增系统的联系人信息" 26 android:onClick="addContact" 27 /> 28 29 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4513189.html