簡體   English   中英

從Android應用發送消息到whatsapp

[英]Sending message to whatsapp from Android app

我們如何從Android應用程序直接將圖像發送到whatsapp? 我嘗試使用

 Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Uri imageuri = getImageUri(getApplicationContext(), b); Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setPackage("com.whatsapp"); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.putExtra(Intent.EXTRA_STREAM, imageuri); sendIntent.setType("image/*"); startActivity(sendIntent); 

上面的代碼打開whatsapp的發送窗口。 還有其他方法可以直接發送圖像而無需打開Whats App窗口嗎?

Android意圖系統

與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");
This would simply be set right before you call startActivity(sendIntent);

同樣,您可以使用Android的Intent系統通過WhatsApp發送媒體,再次使用setPackage將包限制為WhatsApp(如果您只想發送到WhatsApp)。 檢查此開發人員頁面以獲取更多信息。

自定義網址方案

WhatsApp提供了一個自定義URL方案以與WhatsApp進行交互:

如果您有一個網站,並希望使用預填的消息來打開WhatsApp聊天,則可以使用我們的自定義URL方案來進行。 打開whatsapp:// send?text =,后跟要發送的文本,將打開WhatsApp,允許用戶選擇聯系人,並用指定的文本預填充輸入字段。

這是如何在您的網站上編寫此示例:

<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

//非原始文本:您也可以通過Java代碼調用此端點

發現於: https : //www.whatsapp.com/faq/es/android/28000012

暫無
暫無

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

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