首页 文章

Android Intent从代码中调用Whatsapp

提问于
浏览
0

我正在使用此代码直接从我的Call Logs应用程序调用WhatsApp . 这种方法效果很好,但前提是电话号码包含有效的国家/地区代码 . 例如,使用919789006685调用WhatsApp可以正常工作,但使用97890 06685调用它不起作用 . 这里91是国家代码 .

由于我的应用程序从通话记录中读取电话号码,因此无论存储的电话号码格式如何,我都可以使用任何有效的联系电话号码来调用WhatsApp . 用户可能会在其联系人中存储不包含国家/地区代码的号码 . 那么这个问题是否有解决方法?

我的应用中使用的代码:

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

3 回答

  • 0

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

    android-make whatsapp call

    • 检索所选联系人的ID .

    • 从ContactsContract.RawContacts获取联系人ID的所有RAW联系人ID

    • 查询ContactsContract.Data表以查找所有原始联系人并检索列ContactsContract.Data._ID

    • 由于联系人可能在WhatsApp上激活了多个电话号码,您可能需要另外检查列ContactsContract.Data.DATA3以过滤掉您要匹配的电话号码

    • 要启动WhatsApp对话,请使用vnd.android.cursor.item / vnd.com.whatsapp.profile和vnd.android.cursor.item / vnd.com.whatsapp.voip.call,如果您想启动WhatsApp通话

    • 使用步骤3中的ID启动WhatsApp . 这是示例代码:

    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);
    
  • 2
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/html");
            sharingIntent.setPackage("com.whatsapp");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>https://play.google.com/store/apps/details?id=" + context.getPackageName() + "</p>"));
            context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
    
  • 0

    WhatsApp针对特定的电话号码

    val whatsAppIntent = Intent(Intent.ACTION_VIEW)
    val encodedText = URLEncoder.encode("Helo World", "UTF-8")
    whatsAppIntent.data = Uri.parse("http://api.whatsapp.com/send?phone=$phoneNumber&text=$encodedText")
    

相关问题