如何从我的应用程序分享文本到WhatsApp?

问题描述 投票:38回答:9

我开发了一个具有共享文本功能的应用程序。除WhatsApp外,这个工作正常。我该怎么办?是否有任何特定的API?

android android-intent share whatsapp
9个回答
-18
投票

对于什么应用程序没有公共官方api ....所以现在不可能。


100
投票

您可以使用intent来执行此操作。无需使用Whatsapp API。希望我没有误解你的问题。希望有所帮助,谢谢。

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
try {
    activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
    ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

16
投票

有两种方法可以与WhatsApp集成:

  • 通过自定义URL方案
  • 通过Android的意图系统。

如果您有一个网站并希望打开带有预填充消息的WhatsApp聊天,您可以使用我们的自定义URL方案来执行此操作。打开whatsapp:// send?text =后跟要发送的文本,将打开WhatsApp,允许用户选择联系人,并使用指定的文本预填充输入字段。

与Android上的大多数社交应用程序一样,WhatsApp倾向于分享媒体和文本。例如,只需创建共享文本的意图,系统选择器将显示WhatsApp:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

但是,如果您希望直接与WhatsApp共享并绕过系统选择器,则可以在意图中使用setPackage:

sendIntent.setPackage("com.whatsapp");

这只需在调用startActivity(sendIntent)之前设置;

请参考以下链接官方WhatsApp页面:https://www.whatsapp.com/faq/en/android/28000012

如果您想与特定的WhatsApp联系人分享一些文字,请参阅以下代码。

private void openWhatsApp() {
String smsNumber = "7****"; //without '+'
try {
    Intent sendIntent = new Intent("android.intent.action.MAIN");
    //sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);
} catch(Exception e) {
    Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show();
 }

}

有关详细信息,请参阅以下链接Send text to specific contact (whatsapp)


6
投票
Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");
    share.putExtra(Intent.EXTRA_TEXT, "Your text");
    startActivity(Intent.createChooser(share, "Share using"));

2
投票

如果用户的设备中没有Wha​​tsapp应用程序,那么您将获得ActivityNotFoundException

然后你应该先移动用户下载应用程序。

public void shareViaWhatsApp() {
        Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
        whatsappIntent.setType("text/plain");
        whatsappIntent.setPackage("com.whatsapp");
        whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Application of social rating share with your friend");
        try {
            Objects.requireNonNull(getActivity()).startActivity(whatsappIntent);
        } catch (android.content.ActivityNotFoundException ex) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.whatsapp")));
        }
    }

1
投票

我不是100%肯定...但我担心没有官方API发布。我也想实现一个“发送给我们一个whatsapp”的功能,但我放弃了一段时间,直到whatsapp.inc发布官方版本

有一些没有官方的API,但我不知道你是否想要...

http://www.whatsapp-api.com/developers.php

https://github.com/venomous0x/WhatsAPI

祝你好运....如果你发现了什么,请告诉我;)



1
投票
  Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);

0
投票
 message = "this msg is sent from My App Time Track"
            val intent = Intent()//Empty as we don't know the destination i.e implicit intent
            intent.action = Intent.ACTION_SEND//intent will do work of sending something
            intent.putExtra(Intent.EXTRA_TEXT, message)//send given message
            intent.putExtra(Intent.EXTRA_SUBJECT,"Download Time Track App")//give the subject for your message
            //Intent.Extra_Text is actually a globol key
            intent.type = "plane/text"//type of intent

            startActivity(Intent.createChooser(intent,"Send to: "))//createChooser is a dialogBox which shows app available to send data
© www.soinside.com 2019 - 2024. All rights reserved.