Android development-getting mobile phone address book

Source: Internet
Author: User

Android development-getting mobile phone address book

Obtaining the mobile phone address book is the most commonly used small function in Android. I learned it by myself today and wrote it down mainly through the data provided by the content provider provided by the system, we use the content receiver to get the corresponding data to the cursor, and then obtain the fields in the corresponding data table. What is the meaning of the related fields? We can only query them by ourselves.

The following is the code of the mobile phone address book list for reference only:

Package com. andy. phonecontact; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. content. contentResolver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. widget. listView; import android. widget. simpleAdapter;/*** @ author Zhang, Tianyou * @ version November 18, 2014 10:00:46 */public class MainActivity extends Activity {private ListView select_contact; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. activity_main); select_contact = (ListView) findViewById (R. id. select_contact); List
 
  
> Data = getContactInfo (); select_contact.setAdapter (new SimpleAdapter (MainActivity. this, data, R. layout. contact_item_view, new String [] {"name", "phone"}, new int [] {R. id. TV _name, R. id. TV _phone});}/*** read the contact information on the mobile phone ** @ return */private List
  
   
> GetContactInfo () {// put all contacts in listList
   
    
> List = new ArrayList
    
     
> (); // Get a content parser ContentResolver resolver = getContentResolver (); // obtain the content provider url of the contact table raw_contacts table and data table Uri = uri. parse ("content: // com. android. contacts/raw_contacts "); Uri datauri = Uri. parse ("content: // com. android. contacts/data "); Cursor cursor = resolver. query (uri, new String [] {"contact_id"}, null); // obtain contact_id to obtain the contact idwhile (cursor. moveToNext () {String contact_id = cursor. getS Tring (0); if (contact_id! = Null) {// a specific contact Map
     
      
Map = new HashMap
      
        (); // If not empty, query the contact information of the corresponding data table Cursor dataCursor = resolver. query (datauri, new String [] {"data1", "mimetype"}, "contact_id =? ", New String [] {contact_id}, null); while (dataCursor. moveToNext () {String data1 = dataCursor. getString (0); String mimetype = dataCursor. getString (1); System. out. println ("data1 =" + data1 + "mimetype =" + mimetype); if ("vnd. android. cursor. item/phone_v2 ". equals (mimetype) {System. out. println ("Tel:" + data1); map. put ("phone", data1);} else if ("vnd. android. cursor. item/name ". equals (mimetype) {System. out. println ("name:" + data1); map. put ("name", data1) ;}} list. add (map); // release the cursor dataCursor. close () ;}} cursor. close (); return list ;}}
      
     
    
   
  
 

2 Layout file in activity_main.xml

     
  
 

3. ListView filling layout contact_item_view.xml

 
     
      
  
 


4. Add the corresponding permissions:

 
 



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.