- Public class contracttest extends activity {
- Private Static final string tag = "u0fly contracttest ==> ";
- Private Static final int pick_contact = 3;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Log. D (TAG, "oncreate ");
- Textview TV = (textview) findviewbyid (R. Id. textview );
- Button BTN = (button) findviewbyid (R. Id. BTN );
- BTN. setonclicklistener (New View. onclicklistener (){
- @ Override
- Public void onclick (view v ){
- // Todo auto-generated method stub
- Intent intent = new intent (intent. action_pick, contactscontract. Contacts. content_uri );
- Startactivityforresult (intent, pick_contact );
- }
- });
- }
- @ Override
- Public void onactivityresult (INT reqcode, int resultcode, intent data ){
- Super. onactivityresult (reqcode, resultcode, data );
- Log. D (TAG, "onactivityresult ");
- Switch (reqcode ){
- Case (pick_contact ):
- Log. D (TAG, "onactivityresult pick_contact ");
- If (resultcode = activity. result_ OK ){
- Uri contactdata = data. getdata ();
- Contentresolver c = getcontentresolver ();
- Cursor cursor = C. Query (contactdata, null );
- If (cursor. movetofirst ()){
- String [] phones_projection = new string [] {"_ id", "display_name", "data1", "data3 "};
- String contactid = cursor. getstring (cursor. getcolumnindex (phonelookup. _ id ));
- Cursor phone = C. Query (
- Contactscontract. commondatakinds. Phone. content_uri,
- Phones_projection,
- Contactscontract. commondatakinds. Phone. contact_id
- + "=" + Contactid, null, null );
- // Name type ..
- While (phone. movetonext ()){
- Int I = phone. getint (0 );
- String STR = phone. getstring (1 );
- STR + = "";
- STR + = phone. getstring (2 );
- STR + = "";
- STR + = phone. getstring (3 );
- }
- // Other data is available for the contact. I have decided
- // To only get the name of the contact.
- String name = cursor. getstring (cursor. getcolumnindexorthrow (contactscontract. Contacts. display_name ));
- Log. D (TAG, "onactivityresult pick_contact result_ OK 444 ");
- // Toast. maketext (getapplicationcontext (), name, Toast. length_short). Show ();
- Textview TV = (textview) findviewbyid (R. Id. textview );
- TV. settext (name );
- }
- }
- }
- }
- }
Add permission:
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>