簡體   English   中英

如何從我的應用程序將文本分享到 WhatsApp?

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

我開發了一個具有共享文本功能的應用程序。 除了 WhatsApp 之外,這工作正常。 我應該怎么辦? 是否有任何特定的 API?

您可以使用意圖來這樣做。 無需使用 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.");
}

有兩種方法可以與 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();
 }

}

有關更多詳細信息,請參閱下面的鏈接發送文本到特定聯系人(whatsapp)

如果用戶的設備中沒有 Whatsapp 應用程序,則用戶將收到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")));
        }
    }
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);

我不是 100% 確定...但恐怕沒有官方 API 發布。 我還想實現“向我們發送 whatsapp”功能,但我放棄了一段時間,直到 whatsapp.inc 發布官方功能

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

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

https://github.com/venomous0x/WhatsAPI

祝你好運……如果你發現了什么,請告訴我;)

您可以使用 WhatsApp API Android:http: //www.whatsapp.com/faq/en/android/28000012 iOS:http: //www.whatsapp.com/faq/en/iphone/23559013

 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

whats app 沒有公開的官方 api....所以現在不可能了。 (2012 年 11 月 6 日回答)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM