Use contentprovider get contacts

Source: Internet
Author: User

 

Usecontentprovidergetcontactsactivity. Java

 

Package gogler. myandroid;

Import Android. App. activity;
Import Android. content. contentresolver;
Import Android. database. cursor;

Import Android. OS. Bundle;

Import Android. provider. contactscontract;
Import Android. provider. contactscontract. phonelookup;
Import Android. widget. textview;

Public class usecontentprovidergetcontactsactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Textview TV = new textview (this );
String string = "contacts: \ n ";

String number = "";
/* 1. Obtain contentresolver */
Contentresolver Cr = This. getcontentresolver ();
/* 2. Obtain a cursor query object */
Cursor cursor = Cr. Query (contactscontract. Contacts. content_uri, null,
Null, null, null );
/* 3. Traverse contentresolver */
While (cursor. movetonext ()){
/*
* Retrieve the contact name index
*/
Int namefieldcolumnindex = cursor
. Getcolumnindex (phonelookup. display_name );
/* Get the contact name */
String contact = cursor. getstring (namefieldcolumnindex );
/* 4. Create a cursor */
Cursor target = This. getcontentresolver (). Query (
Contactscontract. Contacts. content_uri, null, null,
Null );
/* 5. Locate the cursor to the first line */
Target. movetofirst ();
/* 6. Obtain the contact ID */
String contentid = cursor. getstring (Target
. Getcolumnindex (contactscontract. Contacts. _ id ));
/* 7. Check whether the current contact has a mobile phone number */
String ispnone = cursor
. Getstring (cursor
. Getcolumnindex (contactscontract. Contacts. has_phone_number ));
/* 8. If the current contact has a phone number */
If (integer. parseint (ispnone)> 0 ){
/* 9. Create a cursor-contact ID */
Cursor phone = This. getcontentresolver (). Query (
Contactscontract. commondatakinds. Phone. content_uri,
Null,
Contactscontract. commondatakinds. Phone. contact_id + "="
+ Contentid, null, null );
/* 10. Retrieve multiple mobile phone numbers of the current contact */
While (phone. movetonext ()){
// Obtain the contact number
Number = phone
. Getstring (Phone
. Getcolumnindex (contactscontract. commondatakinds. Phone. Number ));
String + = (contact + ":" + number + "\ n ");
}
}
}
TV. settext (string );
Setcontentview (TV );

}
}

 

Manifest. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "gogler. myandroid"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">

<Uses-SDK Android: minsdkversion = "13"/>
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>

<Application
Android: icon = "@ drawable/Russia"
Android: Label = "@ string/app_name">
<Activity
Android: Label = "@ string/app_name"
Android: Name = ". usecontentprovidergetcontactsactivity">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>

<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>

</Manifest>

 

Code Packaging

Http://download.csdn.net/download/gogler/3725437

 

Demo

 

 

 

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.