Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. database. cursor; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. provider. contactscontract. contacts; <br/> Import android. provider. contactscontract. commondatakinds. phone; <br/> Import android. util. log; <br/> Import android. widget. textview; <br/>/** <br/> * @ Description: obtains information in the user's phone book. <br /> * @ Author chenzheng_java <br/> */<br/> public class contactsreaderactivtiy extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. contactsreader); <br/> querycontactsinfo (); <br/>}</P> <p> private void querycontactsinfo () {<br/>/** <br/> * the phone here is Android. provider. contactscontract. Commondatakinds. phone <br/> */<br/> URI uri = phone. content_uri; <br/> string info = "URI:" + Uri. tostring (); <br/>/*** <br/> * The managedquery method and contentresover are used. when there is a unique difference between query methods, <br/> * The managedquery method adds one more line of startmanagingcursor (cursor ), this means that <br/> * the system automatically manages the life cycle of cursor based on the changes in the lifecycle of our current activity, <br/> * automatically call the corresponding invalidation method deactivate () or re-query the method requery () or close () method <br/> * this is why we didn't explicitly disable cursor here. <Br/> */<br/> cursor = managedquery (Uri, null, null); </P> <p> cursor. movetofirst (); <br/> int COUNT = cursor. getcount (); <br/>/** <br/> * The contacts here is Android. provider. contactscontract. contacts <br/> */<br/> int nameindex = cursor. getcolumnindex (contacts. display_name); <br/> int numberindex = cursor. getcolumnindex (phone. number); <br/> string [] names = cursor. getcolumnnames (); </P> <p> Fo R (INT I = 0; I <count; I ++) {</P> <p> for (string name: names) {<br/> string S = cursor. getstring (cursor. getcolumnindex (name) + ""; <br/> log. I ("column name" + name, S); <br/>}</P> <p> log. I ("notification", "one piece of information has ended. "); </P> <p> info + =" type: "+ cursor. getstring (cursor. getcolumnindex (phone. mimetype); <br/> info + = "name:" + cursor. getstring (nameindex); <br/> info + = "residential phone:" + cursor. getstring (numberindex) + "/N"; <br/> // info + = "phone. number = "+ phone. number + "Index =" + numberindex; <br/> cursor. movetonext (); <br/>}< br/> textview = (textview) findviewbyid (R. id. textview_contacts); <br/> textview. settext (Info); </P> <p >}< br/>
Do not forget to add the permission. <uses-Permission Android: Name = "android. Permission. read_contacts"> </uses-Permission>