The phone book of the android calling system allows you to add, delete, modify, and query data.

Source: Internet
Author: User

To meet this requirement, you need to call the system phone book to add, delete, modify, and query the page and refresh the page in real time. A demo is provided for your reference. The contact name and mobile phone number are displayed on the listview, click the details of the contact, and press and hold the context menu. The edit and delete menu is displayed. See the code below.

First, add the read and write permissions to the phone book in androidmainifest.

<uses-permission android:name="android.permission.READ_CONTACTS" />  <uses-permission android:name="android.permission.WRITE_CONTACTS" />

Because it inherits from listactivity and does not use the configuration file, the Java code is pasted below.

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 = 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 // listen to each item, press show detailed phone content 45 protected void onlistitemclick (listview L, view V, int position, long ID) 46 {47 URI uri = contenturis. withappendedid (contactscontract. contacts. content_uri, ID); 48 // view contact 49 startactivity (new intent (intent. action_view, Uri); 50} 51 52 public Boolean oncreateoptionsmenu (menu) 53 {54 super. oncreateoptionsmenu (menu); 55 // Add the contact 56 menu. add (0, addcontact_id, 0, "add"); 57 Return true; 58} 59 60 public Boolean onoptionsitemselected (menuitem item) 61 {62 switch (item. getitemid () 63 {64 case addcontact_id: 65 // Add a contact 66 intent = new intent (); 67 // set the action attribute for intent (Action: Edit) 68 intent. setaction (intent. action_insert); 69 string data = "content: // COM. android. contacts/contacts "; 70 // parse the URI object 71 URI uri = URI Based on the specified string. parse (data); 72 // set the data attribute 73 intent. setdata (URI); 74 startactivity (intent); 75 getquery (); 76} 77 return Super. onoptionsitemselected (item); 78} 79 80 // long press menu 81 public void oncreatecontextmenu (contextmenu menu, 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 // get the long-pressed data item 93 cursor = (cursor) getlistadapter (). getitem (info. position); 94 If (cursor = NULL) 95 {96 return; 97} 98 menu. setheadertitle (cursor. getstring (1); 99 // add and delete menu 100 menu. add (0, delete, 0, "delete"); 101 menu. add (0, edit, 1, "edit"); 102} 103 104 @ override105 public Boolean oncontextitemselected (menuitem item) 106 {107 adapterview. adaptercontextmenuinfo Info; 108 try109 {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 // delete a record 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 @ override136 protected void onresume () {137 // todo auto-generated method stub138 getquery (); 139 super. onresume (); 140} 141 142} 143
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.