Android意图从代码中调用Whatsapp [英] Android Intent to call Whatsapp from code

查看:380
本文介绍了Android意图从代码中调用Whatsapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码直接从我的通话记录"应用中调用WhatsApp.这很好用,但前提是电话号码包含有效的国家/地区代码.例如,使用919789006685调用WhatsApp可以,但是使用97890 06685调用它却不起作用.这里的91是国家/地区代码.

I am using this code to call WhatsApp directly from my Call Logs app. This works well, but only if the phone number includes a valid country code. For example calling WhatsApp with 919789006685 works but calling it with 97890 06685 does not work. Here 91 is the country code.

由于我的应用程序从呼叫日志中读取电话号码,因此无论存储的电话号码格式如何,我都应该能够使用任何有效的联系电话号码来调用WhatsApp.用户可能会在其联系人中存储不包含国家或地区代码的号码.那么,有没有解决此问题的方法?

Since my app reads phone numbers from Call Logs, I should be able to invoke WhatsApp with any valid contact phone number, irrespective of the stored phone number format. It is possible that users store numbers in their contacts which do not include country codes. So is there a workaround for this issue?

我的应用程序中使用的代码:

Code used within my app:

Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
String waNumber = contactPhone.replace("+", "");
sendIntent.putExtra("jid", waNumber + "@s.whatsapp.net");
startActivity(sendIntent);

推荐答案

这是完整的解决方案.对于从其应用程序尝试相同操作的人可能有用.感谢Sourav Ganguly的链接.

Here is the complete solution. May be useful for people who are trying the same from their apps. Thanks to Sourav Ganguly for the link.

android-make whatsapp调用

  1. 获取所选联系人的ID.
  2. 从ContactsContract.RawContacts获取所有RAW联系人ID的联系人ID.
  3. 查询所有原始联系人的ContactsContract.Data表并检索ContactsContract.Data._ID列
  4. 由于联系人可能在WhatsApp上具有多个活动的电话号码,因此您可能需要另外检查ContactsContract.Data.DATA3列以过滤出要匹配的电话号码
  5. 要启动WhatsApp对话,如果要启动WhatsApp调用,请使用vnd.android.cursor.item/vnd.com.whatsapp.profile和vnd.android.cursor.item/vnd.com.whatsapp.voip.call
  6. 使用第3步中的ID启动WhatsApp.这是示例代码:

  1. Retreive the ID for the selected Contact.
  2. Get all the RAW Contact Id's for the contact id from ContactsContract.RawContacts
  3. Query the ContactsContract.Data table for all Raw Contacts and retrieve the column ContactsContract.Data._ID
  4. Since a contact may have several phone numbers active on WhatsApp, you may want to check additionally the column ContactsContract.Data.DATA3 to filter out the phone number you want to match
  5. To start a WhatsApp conversation use vnd.android.cursor.item/vnd.com.whatsapp.profile and vnd.android.cursor.item/vnd.com.whatsapp.voip.call if you want to start a WhatsApp call
  6. Use the ID from step 3 to start WhatsApp. Here is sample code:

String data = "content://com.android.contacts/data/" + dataId;
            String type = "vnd.android.cursor.item/vnd.com.whatsapp.profile";
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_VIEW);
            sendIntent.setDataAndType(Uri.parse(data), type);
            sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);

这篇关于Android意图从代码中调用Whatsapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆