从联系人详细信息自定义行打开应用程序时获取联系人信息

时间:2019-07-02 17:12:12

标签: android android-contentprovider android-contacts

我已经在联系人详细信息中创建了一个自定义行,并带有一个自定义的mimetype,并将该mimetype与我的应用中的活动相关联

<activity
        android:name=".xxxx.xxxxxActivity"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.cursor.item/com.sample.profile2"/>
        </intent-filter>
    </activity>

enter image description here

当我单击联系人详细信息中的“示例”行时,它会在正确的活动中打开我的应用程序(以前关闭),但我不知道如何从该意图的来源获取联系信息。

如何在我的应用程序上获取联系信息?例如电话号码。

编辑 如果有帮助,这就是我将自定义行添加到联系人详细信息的方式

fun addContact(context: Context, contact: Contact) {
    val resolver = context.contentResolver
    resolver.delete(ContactsContract.RawContacts.CONTENT_URI,
            ContactsContract.RawContacts.ACCOUNT_TYPE + " = ?", arrayOf(AccountGeneral.ACCOUNT_TYPE))

    //...


    val ops = ArrayList<ContentProviderOperation>()

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true))
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
            .build())

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Settings.CONTENT_URI, true))
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
            .withValue(Settings.UNGROUPED_VISIBLE, 1)
            .build())

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Contacts.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.GIVEN_NAME, givenName)
            .withValue(StructuredName.FAMILY_NAME, familyName)
            .build())

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
            .withValueBackReference(ContactsContract.Contacts.Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.aliasName.getFormattedPhoneNumber())
            .build())

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
            .withValueBackReference(ContactsContract.Contacts.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Contacts.Data.MIMETYPE, MIMETYPE)
            .withValue(ContactsContract.Contacts.Data.DATA1, Integer.valueOf(14))
            .withValue(ContactsContract.Contacts.Data.DATA2, "sample")
            .withValue(ContactsContract.Contacts.Data.DATA3, "sample")
            .build())

    try {
        val results = resolver.applyBatch(ContactsContract.AUTHORITY, ops)
        if (results.isEmpty())
            return
    } catch (e: Exception) {
        e.printStackTrace()
    }


private fun addCallerIsSyncAdapterParameter(uri: Uri, isSyncOperation: Boolean): Uri {
    return if (isSyncOperation) {
        uri.buildUpon()
                .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
                .build()
    } else uri
}

EDIT2 使用以下代码

val intent = intent
if (intent.data != null) {
    Logger.e("intent received", intent.data!!.toString())

    val uri = intent.data
    val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)

    val cursor = applicationContext.contentResolver.query(uri, projection, null, null, null)
    cursor!!.moveToFirst()

    val numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
    val number = cursor.getString(numberColumnIndex)

    val nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
    val name = cursor.getString(nameColumnIndex)

    Logger.e("name number", "$number $name")
    cursor.close()
} else {
    Logger.e("intent received", "null intent received")
}

我在日志中得到了

E/intent received: content://com.android.contacts/data/5875
E/name number: 14 John Doe

不确定这14个代表什么。但这不是电话号码。 ContactsContract.CommonDataKinds.Phone.NUMBER不能正确地获取电话号码吗?

0 个答案:

没有答案