我开发了一个可以共享文本的应用程序。除了 WhatsApp 之外,这个功能都运行良好。我该怎么做?有没有专门的 API 可以解决这个问题?
我开发了一个可以共享文本的应用程序。除了 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.");
}
com.whatsapp.w4b
的 Whatsapp 商业应用程序是无效的。因为现在许多人开始使用 Whatsapp 商业应用程序。 - Rumit Patel有两种与 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)
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。
有一些非官方的API,但我不知道你是否需要...
http://www.whatsapp-api.com/developers.php
https://github.com/venomous0x/WhatsAPI
祝你好运……如果你发现了什么,请告诉我;)
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
WhatsApp目前没有公开的官方API,因此现在还不可能实现。(回答时间:2012年11月6日)