简体   繁体   English

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

[英]How to share text to WhatsApp from my app?

I develop an app with a functionality for sharing text.我开发了一个具有共享文本功能的应用程序。 This is working fine except for WhatsApp.除了 WhatsApp 之外,这工作正常。 What should I do?我应该怎么办? Is there any specific API for that?是否有任何特定的 API?

You can use intent to do so.您可以使用意图来这样做。 No need to use Whatsapp API.无需使用 Whatsapp API。 Hope that I have not misunderstood your question.希望我没有误解你的问题。 Hope that helps, thanks.希望有帮助,谢谢。

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.");
}

There are two ways to integrate with WhatsApp:有两种方法可以与 WhatsApp 集成:

  • Through a custom URL scheme通过自定义 URL 方案

  • Through Android's intent system.通过 Android 的意图系统。

If you have a website and want to open a WhatsApp chat with a pre-filled message, you can use our custom URL scheme to do so.如果您有一个网站并想打开带有预填消息的 WhatsApp 聊天,您可以使用我们的自定义 URL 方案来实现。 Opening whatsapp://send?text= followed by the text to send, will open WhatsApp, allow the user to choose a contact, and pre-fill the input field with the specified text.打开 whatsapp://send?text= 后跟要发送的文本,将打开 WhatsApp,允许用户选择联系人,并使用指定的文本预填充输入字段。

Like most social apps on Android, WhatsApp listens to intents to share media and text.与 Android 上的大多数社交应用程序一样,WhatsApp 会听取共享媒体和文本的意图。 Simply create an intent to share text, for example, and WhatsApp will be displayed by the system picker:例如,只需创建一个共享文本的意图,系统选择器就会显示 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);

However, if you prefer to share directly to WhatsApp and bypass the system picker, you can do so by using setPackage in your intent:但是,如果您更喜欢直接分享到 WhatsApp 并绕过系统选择器,您可以通过在您的意图中使用 setPackage 来实现:

sendIntent.setPackage("com.whatsapp");

This would simply be set right before you call startActivity(sendIntent);这将在您调用 startActivity(sendIntent) 之前简单地设置;

Please refer below link Official WhatsApp Page: https://www.whatsapp.com/faq/en/android/28000012 ,请参考以下链接官方 WhatsApp 页面: https ://www.whatsapp.com/faq/en/android/28000012,

If you want to share some text to specific WhatsApp contact, Please refer below code.如果您想与特定的 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();
 }

}

For more details please refer below link Send text to specific contact (whatsapp)有关更多详细信息,请参阅下面的链接发送文本到特定联系人(whatsapp)

If the user does not have the Whatsapp app in their device then the user will get the ActivityNotFoundException如果用户的设备中没有 Whatsapp 应用程序,则用户将收到ActivityNotFoundException

Then, you should move the user to download the app first.然后,你应该让用户先下载应用程序。

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")));
        }
    }
Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");
    share.putExtra(Intent.EXTRA_TEXT, "Your text");
    startActivity(Intent.createChooser(share, "Share using"));
  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);

I am not 100% sure...but i am afraid there is no official API released.我不是 100% 确定...但恐怕没有官方 API 发布。 I also wanted to implement a "send us a whatsapp" feature, but i am giving up for a while until whatsapp.inc release a official one我还想实现“向我们发送 whatsapp”功能,但我放弃了一段时间,直到 whatsapp.inc 发布官方功能

There are some no-official API but i do not know if you want that...有一些非官方的 API,但我不知道你是否想要那个......

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

https://github.com/venomous0x/WhatsAPI https://github.com/venomous0x/WhatsAPI

good luck....and if you discover something, please let me know;)祝你好运……如果你发现了什么,请告诉我;)

 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

There is no public official api for whats app....So it is not possible now. whats app 没有公开的官方 api....所以现在不可能了。 (answered on 6-Nov-2012) (2012 年 11 月 6 日回答)

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

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