1

What I'm trying to achieve is to add shortcut to my app in android book contact details, similar to what whatsapp is doing.

I've been following this tutotial: http://blogs.quovantis.com/syncing-contacts-with-an-android-application-2/ and it works well but the author doesn't show how to pass data from contact details to ViewingActivity: https://github.com/ajkh35/ContactsDemo/blob/master/app/src/main/java/com/example/ajay/contacts_4/ViewingActivity.java

There was some comments below the article but no specific answer from the author, can't find anything useful in

    Uri data = getIntent().getData(); //content://com.android.contacts/data/1169
    List<String> params = data.getPathSegments();
    String first = params.get(0); 
    String second = params.get(1);

there is some number passed in second param but it's not CONTACT_ID or RAW_CONTACT_ID. Any help?

marmor
  • 27,641
  • 11
  • 107
  • 150
Karol Mac
  • 11
  • 2
  • @marmor It's not duplicate , seen this question before, doesn't solve my problem in any way – Karol Mac May 22 '19 at 10:59
  • ok, un-duplicated it, can you please add more information / screenshot to what you're trying to accomplish? as it sounds the same as my link – marmor May 22 '19 at 13:23
  • I completed mentioned tutorial with success , so I have my app launch icon in contact details, now when I press icon ViewingActivity appears but I can't by any means retrieve CONTACT_ID or RAW_CONTACT_ID of contact from which I launched ViewingActivity. Is that clear now? – Karol Mac May 22 '19 at 13:40

2 Answers2

0

Ok, so it seems the Uri you're getting from the Contacts app is a Data uri.

Data rows contain info about a specific data-item (like a phone number or an email) of a specific RawContact, so a single Data row "belongs" to a single RawContact which "belongs" to a single Contact.

Luckily, the ContactsContract API allows for implicit joins when querying the Data table, so you can do something like this:

Uri dataUri = getIntent().getData(); //content://com.android.contacts/data/1169

String[] projection = new String[]{
        Data.CONTACT_ID,
        Data.RAW_CONTACT_ID,
        Data.DISPLAY_NAME,
        Data.MIMETYPE,
        Data.DATA1};

Cursor cur = getContentResolver().query(dataUri, projection, null, null, null);
cur.moveToFirst(); // there should always be exactly one result, since we have a specific data uri here

Log.i("Contact Info", "Got info: id=" + cur.getLong(0) + ", raw-id=" + cur.getLong(1) + ", " + cur.getString(2) + ", " + cur.getString(3) + ", " + cur.getString(4));

cur.close();
marmor
  • 27,641
  • 11
  • 107
  • 150
0

I know this is a very late response but checkout the following code.

class MessageActivity : AppCompatActivity() {

private val TAG: String = javaClass.simpleName

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_message)

    if(intent != null && intent.data != null) {
        Log.e(TAG, intent.data.toString())

        var contactName = ""
        val cursor = contentResolver.query(intent.data!!,
            arrayOf(ContactsContract.Data.DATA1,ContactsContract.Data.DATA2,ContactsContract.Data.DATA3),
            null,null,null)

        if(cursor != null && cursor.moveToFirst()) {
            do{
                Log.e(TAG, cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactsContract.Data.DATA1)))
                contactName = cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactsContract.Data.DATA2))
                Log.e(TAG, contactName)
                Log.e(TAG, cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactsContract.Data.DATA3)))
            }while (cursor.moveToNext())
            cursor.close()
        }

        messaging_text.text = getString(R.string.messaging) + " $contactName"
    }
}}

So when you register a contact you set some Data1, Data2 and Data3 values. Data3 is what gets displayed in the contacts. You can set Data1 and Data2 some value you like and then retrieve it like in the code I mentioned above.

You can also checkout my blog here. Look for the "Sync Service" section, towards the end you will find the MessageActivity.

Thanks & regards