0

my app must picks one contact.

mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent pickContact = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
                startActivityForResult(pickContact, REQUEST_CONTACT);
            }
        });

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CONTACT && intent != null) {
            Uri contactUri = intent.getData();
            String[] queryFields = new String[]{Contacts.DISPLAY_NAME_PRIMARY} ;
            Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null);
            try {
                if (c.getCount() == 0) {
                    return;
                }
                c.moveToFirst();
                String contact = c.getString(0);
                mButton.setText(contact);
            } finally {
                c.close();
            }
        }
    }

But it works fine only with contacts stored in my phone. If contact stored in Google account, it wouldn't work. I'll get empty cursor. I've read this and this. And i've read Google Contacts API. These methods are completely different. What should i do if i want get data from any contacts (Google, phone, sim) by pressing single button?

Community
  • 1
  • 1
  • what is your `contactUri` you want to `query`? – pskink Jan 12 '17 at 09:44
  • i think Contacts.CONTENT_URI. Here `Intent pickContact = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(pickContact, REQUEST_CONTACT);` For my purposes I need only the name of the contact. – Vladimir Telyatyev Jan 12 '17 at 11:05
  • so you dont know that? just call `Log.d` inside `onActivityResult` and watch the logcat – pskink Jan 12 '17 at 11:06
  • `import android.provider.ContactsContract.Contacts;` – Vladimir Telyatyev Jan 12 '17 at 11:12
  • call `Log.d(TAG, "uri: " + intent.getData());` inside `onActivityResult` and watch the logcat – pskink Jan 12 '17 at 11:13
  • `01-12 14:19:29.846 26190-26190/com.example.hogust.criminalintent D/TAG: uri: content://com.android.contacts/contacts/lookup/3789r326-5C0A7C346CBA0A.1853i326/1309 01-12 14:21:03.986 26190-26190/com.example.hogust.criminalintent D/TAG: uri: content://com.android.contacts/contacts/lookup/2408i6955d55a890e0f92/108` the first gives an empty cursor. the second is all right. – Vladimir Telyatyev Jan 12 '17 at 11:21
  • try this http://pastebin.com/be8RPCAN and watch the logcat again – pskink Jan 12 '17 at 11:22
  • `01-12 14:29:44.566 26190-26190/com.example.hogust.criminalintent D/TAG: onActivityResult all data uri content://com.android.contacts/contacts/lookup/3789r326-5C0A7C346CBA0A.1853i326/1309/data 01-12 14:30:12.666 26190-26190/com.example.hogust.criminalintent D/TAG: onActivityResult all data uri content://com.android.contacts/contacts/lookup/2408i6955d55a890e0f92/108/data ` – Vladimir Telyatyev Jan 12 '17 at 11:31
  • what does `DatabaseUtils.dumpCursor(c);` show (line 16 in the code)? – pskink Jan 12 '17 at 11:41
  • in debugger i found this ` >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@41bd0f08` – Vladimir Telyatyev Jan 12 '17 at 11:53
  • so `c.getCount()` == 0? – pskink Jan 12 '17 at 11:54
  • WOW!!! It works perfectly. So, what is my mistake? Generally it was the task from the book. – Vladimir Telyatyev Jan 12 '17 at 12:04
  • i have no idea what book you mean – pskink Jan 12 '17 at 12:05
  • In any case, thank you very much. – Vladimir Telyatyev Jan 12 '17 at 12:07

0 Answers0