Read the phone number from the phone book

Source: Internet
Author: User

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>

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.