【发布时间】:2012-10-08 19:20:19
【问题描述】:
我开发了一个具有共享文本功能的应用程序。除了 WhatsApp,这一切正常。我该怎么办?有什么特定的 API 吗?
【问题讨论】:
-
使用安卓的分享机制。我可以用 WhatsApp 很好地分享文字。
标签: android android-intent share whatsapp
我开发了一个具有共享文本功能的应用程序。除了 WhatsApp,这一切正常。我该怎么办?有什么特定的 API 吗?
【问题讨论】:
标签: android android-intent share whatsapp
我不是 100% 确定...但恐怕没有官方 API 发布。我还想实现一个“向我们发送 whatsapp”功能,但我暂时放弃了,直到 whatsapp.inc 发布正式的功能
有一些非官方的API,但我不知道你是否想要...
http://www.whatsapp-api.com/developers.php
https://github.com/venomous0x/WhatsAPI
祝你好运……如果你发现了什么,请告诉我;)
【讨论】:
whats app 没有公开的官方 api....所以现在不可能。 (2012 年 11 月 6 日回答)
【讨论】:
您可以使用意图来执行此操作。 无需使用 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 API 安卓:http://www.whatsapp.com/faq/en/android/28000012 iOS:http://www.whatsapp.com/faq/en/iphone/23559013
【讨论】:
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);
【讨论】:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "Your text");
startActivity(Intent.createChooser(share, "Share using"));
【讨论】:
有两种方法可以与 WhatsApp 集成:
通过自定义 URL 方案
通过 Android 的 Intent 系统。
如果您有一个网站并希望通过预先填写的消息打开 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)
【讨论】:
如果用户的设备中没有 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")));
}
}
【讨论】:
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
【讨论】: