【问题标题】:How can I open WhatsApp's conversation activity using contact data?如何使用联系人数据打开 WhatsApp 的对话活动?
【发布时间】:2013-04-20 14:00:16
【问题描述】:

我想从我的应用打开什么是应用对话活动cmp=com.whatsapp/.Conversation

我该怎么做?我有联系电话号码、联系人 ID、联系人原始 ID,还有特定联系人的应用 URI。

private void openWhatsApp(String id) {

    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/"+id));
    Log.v("ssssss", s);
    i.setType("vnd.android.cursor.item/vnd.com.whatsapp.profile");
    i.setComponent(new ComponentName("com.whatsapp", ".Conversation"));
    startActivity(i);
}


04-20 18:13:45.794: I/ActivityManager(1862): START
{act=android.intent.action.VIEW
dat=content://com.android.contacts/data/8269
typ=vnd.android.cursor.item/vnd.com.whatsapp.profile
cmp=com.whatsapp/.accountsync.ProfileActivity} from pid 32159


04-20 18:42:11.317: I/ActivityManager(1862): START {flg=0x14000000 cmp=com.whatsapp/.Conversation (has extras)} from pid 1150

【问题讨论】:

  • 添加简要描述和代码大大改善了这个问题从“我想要代码”到“我试过这个,你能帮忙吗?” (这在这里有很大的不同。)
  • @Sam 感谢您的建议。我会记住这一点的。
  • 不清楚你在问什么。代码是否没有启动正确的应用程序? (logcat 的输出另有说明。)它崩溃了吗?还有什么?

标签: android android-intent whatsapp


【解决方案1】:
private void openWhatsApp(String id) {

Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
        new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
        new String[] { id }, null);
c.moveToFirst();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));

startActivity(i);
c.close();
}

其中 id 是什么是应用程序 uri,例如 0987654321@s.whatsapp.net

【讨论】:

  • ...如何获取 id 。我认为这是什么 id 。当我使用您的代码时。我有一个 getSherlockActivity() 错误。我是 android 新手,所以我不明白什么是 getSherlocactivity 以及它在哪里使用。我只想将消息从我的应用程序发送到 whatsup 而不打开它可能与否
  • phoneNo@s.whatsapp.net 是什么应用 ID。或者您可以从contacts.db 中的data1 列获取它。如果不打开 whats 应用程序,就无法从您的应用程序向 whatsup 发送消息。
  • @MahiSingh 我只是删除 getSherlockActivity() 并且它工作正常。
  • @johnsmith 您是如何找到这些信息的?这是官方的吗?类似的代码是否适用于其他社交网络和应用程序,例如 FB-messenger、Viber、Line、Telegram?
  • 这段代码给了我找不到包的异常!!找不到任何应用程序来处理意图。
【解决方案2】:

试试这个代码:

String smsNumber="919426640584@s.whatsapp.net";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "Prakash Gajera");
i.setPackage("com.whatsapp");
startActivity(i);

【讨论】:

  • 需要联系人的contactId,不能使用短信号码。所以它应该看起来像 189202@s.whatsapp.net ,其中 189202 是联系人的 ID。而且这只会启动联系人资料,WhatsApp 仍然会忽略 sms_body 和其他 EXTRA_TEXT 值。
【解决方案3】:

当用户不知道联系号码时,我的最终解决方案。

您也可以设置预格式化文本。

    try {
        String whatsAppRoot = "http://api.whatsapp.com/";
        String number = "send?phone=+xxxxxxxxxxx"; //here the mobile number with its international prefix
        String text = "&text=HERE YOUR TEXT";
        String uri = whatsAppRoot+number+text;

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(uri));
        startActivity(intent);
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), 
        "WhatsApp cannot be opened", Toast.LENGTH_SHORT).show();
    }

【讨论】:

    【解决方案4】:

    你可以使用这个例子

    startActivity(new Intent(Intent.ACTION_VIEW,
                                Uri.parse(
                                        "https://api.whatsapp.com/send?phone=+628119xxx&text=I'm%20interested%20in%20your%20car%20for%20sale"
                                )));
    

    【讨论】:

      【解决方案5】:
       String KEY_QUICK_REPLY_TEXT = "Dear Valued Customer Thank you for contacting us your reference Number is "+refernceNumber ;
         Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setData(Uri.parse("http://api.whatsapp.com/send?phone="+phone +"&text="+KEY_QUICK_REPLY_TEXT));
                                  startActivity(intent);
      
      Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-26
        相关资源
        最近更新 更多