0

As simple as the question. I'd like to know if there is a common intent to show the contacts app, so that the user could see the contacts list.

I've noticed there are intents for picking a contact, showing a contact, add and edit a contact, but all of those are for a single contact (all written here)...

I guess I could query one of those intents, and see which app handles it, and just launch it, but is there maybe a better way?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Hi, have a look at the [link](https://developer.android.com/training/contacts-provider/retrieve-names.html) from the Android developer page. There is a sample to download. – Vall0n Sep 27 '16 at 08:36
  • @Vall0n Not relevant. I didn't ask for getting a list of contacts. I asked if there is an intent to go to the address book app. – android developer Sep 27 '16 at 08:56
  • Oh, Sorry! My bad. – Vall0n Sep 27 '16 at 09:01
  • @Vall0n Well the other answers here also got this mistake somehow, even though I specifically wrote what I'm searching for... One even got upvotes... :( – android developer Sep 27 '16 at 09:15
  • see `Intent`'s category `"android.intent.category.APP_CONTACTS"` – pskink Oct 02 '16 at 05:03
  • @pskink You mean new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_CONTACTS) . It works, but it warns that API 15 or above is needed. How did it work on previous versions of Android? I've written it as an answer. Please reply there. – android developer Oct 03 '16 at 12:02
  • see `Intent#ACTION_VIEW` docs – pskink Oct 03 '16 at 12:51

2 Answers2

0

You can use the below code snippet

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

Add this code in your onActivityResult

@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;
}
}
Jaydroid
  • 334
  • 3
  • 13
0

OK, found the answer using "pskink" user's comment:

startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_CONTACTS));

However, the IDE warns that it requires API 15 and above. I wonder how it worked before. For example, how launchers showed an app icon for this, knowing which app would open it.


EDIT:

For previous versions of Android, you can use:

startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("content://contacts/people")))
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • You mean I should put a URI ? but which to put? Using .setData(Uri.fromParts("tel", "", null))) , it will open the phone app, not the contacts app... – android developer Oct 03 '16 at 20:00
  • read `Intent` docs, you will find the answer for your question – pskink Oct 03 '16 at 20:13
  • 1
    @pskink I can't find a clue for this. Please write as an answer instead. – android developer Oct 05 '16 at 06:56
  • just find all the places where `ACTION_VIEW` is used in `android.content.Intent` class documentation – pskink Oct 05 '16 at 06:59
  • 1
    Found it: startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("content://contacts/people"))) . You should be more direct with your answers. You tend to give the very minimal clue and this wastes time. Please avoid this behavior. – android developer Oct 06 '16 at 05:49
  • no, SO does not work that way, nobody here is going to do your homework, i said to read the docs and you finally found it, so you did your homework – pskink Oct 06 '16 at 05:52
  • @pskink Sure it works this way. There is an "Post Your Answer" button, where you put your answer. It's not homework. It has nothing to do with homework. It's just a question. There is no list of intents that includes the answer to the question, so this is not trivial matter and I can ask this question and people can answer it. Looking at SO, I couldn't find this question asked, so I asked it myself. You should feel free to ask questions here. – android developer Oct 07 '16 at 11:14
  • 1
    @pskink Yes. Each time I can't find an answer here or anywhere else, I ask. Why be shy ? There is a saying "if you don't ask, how would you know?". Asking is one of the main ways to get information others got before. You can check my other questions too. Here're some others that I couldn't find an answer to : http://stackoverflow.com/q/39685835/878126 http://stackoverflow.com/q/39383183/878126 – android developer Oct 07 '16 at 11:48
  • On Android 11+ you'll need: `````` – M66B Oct 27 '21 at 13:02