android调用系统电话薄,实现增删改查。
因为有个需求,需要调用系统电话薄,实现增删改查后并实时刷新页面,写了个demo供大家参考,取联系人名字跟手机号码展示在Listview上,单击出现联系人的详细情况,长按出现上下文菜单,弹出编辑删除菜单,下面请看代码。
首先在Androidmainifest中添加对电话薄的读写权限。
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
因为继承于ListActivity,没有用到配置文件,下面贴出java代码。
1 package com.android; 2 import android.app.ListActivity; 3 import android.content.ContentUris; 4 import android.content.Intent; 5 import android.database.Cursor; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.provider.Contacts.People; 9 import android.provider.ContactsContract; 10 import android.view.ContextMenu; 11 import android.view.Menu; 12 import android.view.MenuItem; 13 import android.view.View; 14 import android.view.ContextMenu.ContextMenuInfo; 15 import android.widget.AdapterView; 16 import android.widget.ListView; 17 import android.widget.SimpleCursorAdapter; 18 19 public class TkActivity extends ListActivity { 20 private static final int AddContact_ID = Menu.FIRST; 21 private static final int delete = Menu.FIRST+1; 22 private static final int edit = Menu.FIRST+2; 23 /** Called when the activity is first created. */ 24 @Override 25 public void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 getListView().setOnCreateContextMenuListener(this); 28 getquery(); 29 } 30 31 public void getquery(){ 32 @SuppressWarnings("deprecation") 33 Cursor cursor = this.getContentResolver().query(People.CONTENT_URI, null, 34 null, null, null); 35 @SuppressWarnings("deprecation") 36 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 37 android.R.layout.simple_list_item_2, 38 cursor, 39 new String[] {People.NAME, People.NUMBER }, 40 new int[] { android.R.id.text1, android.R.id.text2 }); 41 setListAdapter(adapter); 42 } 43 44 //监听每个item,按下显示详细电话内容 45 protected void onListItemClick(ListView l, View v, int position, long id) 46 { 47 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); 48 //查看联系人 49 startActivity(new Intent(Intent.ACTION_VIEW, uri)); 50 } 51 52 public boolean onCreateOptionsMenu(Menu menu) 53 { 54 super.onCreateOptionsMenu(menu); 55 //添加联系人 56 menu.add(0, AddContact_ID, 0, "添加"); 57 return true; 58 } 59 60 public boolean onOptionsItemSelected(MenuItem item) 61 { 62 switch (item.getItemId()) 63 { 64 case AddContact_ID: 65 //添加联系人 66 Intent intent = new Intent(); 67 //为Intent设置Action属性(动作为:编辑) 68 intent.setAction(Intent.ACTION_INSERT); 69 String data = "content://com.android.contacts/contacts"; 70 //根据指定字符串解析出Uri对象 71 Uri uri = Uri.parse(data); 72 //设置Data属性 73 intent.setData(uri); 74 startActivity(intent); 75 getquery(); 76 } 77 return super.onOptionsItemSelected(item); 78 } 79 80 //长按菜单 81 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) 82 { 83 AdapterView.AdapterContextMenuInfo info; 84 try 85 { 86 info = (AdapterView.AdapterContextMenuInfo) menuInfo; 87 } 88 catch (ClassCastException e) 89 { 90 return; 91 } 92 //得到长按的数据项 93 Cursor cursor = (Cursor) getListAdapter().getItem(info.position); 94 if (cursor == null) 95 { 96 return; 97 } 98 menu.setHeaderTitle(cursor.getString(1)); 99 //添加删除菜单 100 menu.add(0, delete, 0, "删除"); 101 menu.add(0,edit,1,"编辑"); 102 } 103 104 @Override 105 public boolean onContextItemSelected(MenuItem item) 106 { 107 AdapterView.AdapterContextMenuInfo info; 108 try 109 { 110 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 111 } 112 catch (ClassCastException e) 113 { 114 return false; 115 } 116 117 switch (item.getItemId()) 118 { 119 case delete: 120 { 121 //删除一条记录 122 Uri noteUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, info.id); 123 getContentResolver().delete(noteUri, null, null); 124 getquery(); 125 return true; 126 } 127 case edit: 128 Uri editUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, info.id); 129 startActivity(new Intent(Intent.ACTION_EDIT, editUri)); 130 getquery(); 131 } 132 return false; 133 } 134 135 @Override 136 protected void onResume() { 137 // TODO Auto-generated method stub 138 getquery(); 139 super.onResume(); 140 } 141 142 } 143