4

As the topic. I researched all day long and know I know that I can add contacts by using native phone book, send SMS via native sms application.

But I can't find any way to use native phone book to search for contacts. I tried working with documentation: http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#SEARCH_SUGGESTION_CLICKED

And all common things related to that.

My question is:

Is it possible to use native phone book to search for contacts and retrieve them? If so, example much appreciated.

Mateusz Zając
  • 316
  • 5
  • 19

2 Answers2

3

I found a solution to my problem thanks to my friend that linked me the same question on stackoverflow.

Basically to get contact by using vendor phonebook search app we must start intent in order to start our journey for searching.

I used it inside a button that calls this method on button click.

public void showContactsChooser(final View view){
   Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
   startActivityForResult(intent, PICK_CONTACT);
}

We now get a screen that is showing us all the contacts we have. We choose one and we are getting back to our app.

To read this contact I am using this method:

    @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 = getContentResolver().query(contactData, null, null, null, null);

                 if (c.moveToFirst()){
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                 Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
                 }
             }
        }
    }

And done :)

My knowledge comes from http://eclipsed4utoo.com/blog/android-open-contacts-activity-return-chosen-contact/ And to the author of that is going all the credit.

Mateusz Zając
  • 316
  • 5
  • 19
1

If you want to search something in phone contact-book, you have to have READ_CONTACTS permission in your AndroidManifest.xml. There you have instruction how to get contacts from phone database. Get intresting phone number and use that code to send your message by default app (you don't need SEND_SMS permission to that. but remember that user will have to accept that message by clicking "Send"):

Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + phoneNumber ) );
intent.putExtra( "sms_body", smsBody );
context.startActivity( intent );
TN888
  • 7,659
  • 9
  • 48
  • 84
  • That's not really what I was asking for. I know how to search phone book I am doing it already. I just want to skip writing my own complicated and good looking phone searching activity and use native. – Mateusz Zając Jul 26 '13 at 10:39
  • @MateuszZając You want use native (what ?) [Czego "native" chcesz użyć ?] – TN888 Jul 26 '13 at 10:44
  • 1
    Speaking without using word native I want to open original phone book that is installed on android from google. [Krótko mówiąc, chcę użyć oryginalnej książki telefonicznej, otworzyć w niej przeglądanie kontaktów i wybrać z niej kontakt. Chcę to zrobić by uniknąć nastu godzin pisania własnej aktywności]. My friend probably found me an answer to this question. And it is here: http://stackoverflow.com/questions/5853878/how-to-open-phonebook-from-android-application – Mateusz Zając Jul 26 '13 at 10:56
  • @MateuszZając I think that your friend found correct solution. Now you can (if you only want) answer your question. [Myślę, że Twój przyjaciel znalazł dobre rozwiązanie i możesz go spokojnie użyć bez uprawnienia `READ_CONTACTS`. Jeśli chcesz, sam odpowiedz na swoje pytanie] – TN888 Jul 26 '13 at 10:59