How to open contacts when I click the button defined in main.xml

I am developing a gps tracking app in android. I ended up displaying a map. Now I want to make a button on top, which, when pressed, will display the contacts. Then, when I select a contact, it should show me its location. Please help me with this. Thanks.

+6
source share
3 answers

You can set the "Event at the click of a button" button by setting OnClickListener on the button with the following code and using Intent to call ContactPicker activity:

 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent= new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT); } }); 

and onActivityResult() process the uri contact to load contact details.

 @Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (PICK_CONTACT) : if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = managedQuery(contactData, null, null, null, null); if (c.moveToFirst()) { String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // TODO Fetch other Contact details as you want to use } } break; } } 
+17
source

You must use startActivityForResult

 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); 

For more information, see "get contact information from android contact picker" .

+11
source

try this code

 Intent intent = new Intent(Intent.ACTION_DEFAULT, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); 

Use ACTION_DEFAULT instead of ACTION_PICK .

Good luck.

0
source

Source: https://habr.com/ru/post/912115/


All Articles