简体   繁体   English

将电话号码或 CONTACT_ID 从 android 联系方式传递给活动(whatsapp 之类)

[英]Passing phone number or CONTACT_ID from android contact details to activity (whatsapp like)

What I'm trying to achieve is to add shortcut to my app in android book contact details, similar to what whatsapp is doing.我想要实现的是在 android book 联系方式中为我的应用程序添加快捷方式,类似于 whatsapp 正在做的事情。

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我一直在关注这个教程: http : //blogs.quovantis.com/syncing-contacts-with-an-android-application-2/它运行良好,但作者没有展示如何从联系人详细信息传递数据到查看活动: 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.在第二个参数中传递了一些数字,但它不是 CONTACT_ID 或 RAW_CONTACT_ID。 Any help?有什么帮助吗?

Ok, so it seems the Uri you're getting from the Contacts app is a Data uri.好的,您从“ Contacts应用程序获得的Uri似乎是一个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 . Data行包含有关特定RawContact的特定数据项(如电话号码或电子邮件)的RawContact ,因此单个Data行“属于”单个RawContact ,后者“属于”单个Contact

Luckily, the ContactsContract API allows for implicit joins when querying the Data table, so you can do something like this:幸运的是, ContactsContract API 允许在查询Data表时进行隐式连接,因此您可以执行以下操作:

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();

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.因此,当您注册联系人时,您会设置一些 Data1、Data2 和 Data3 值。 Data3 is what gets displayed in the contacts. Data3 是在联系人中显示的内容。 You can set Data1 and Data2 some value you like and then retrieve it like in the code I mentioned above.您可以设置 Data1 和 Data2 一些您喜欢的值,然后像我上面提到的代码一样检索它。

You can also checkout my blog here .你也可以在这里查看我的博客。 Look for the "Sync Service" section, towards the end you will find the MessageActivity.查找“同步服务”部分,最后您会找到 MessageActivity。

Thanks & regards感谢和问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM