如何在您的 Android 应用程序中使用 Intent 打开 WhatsApp
Posted
技术标签:
【中文标题】如何在您的 Android 应用程序中使用 Intent 打开 WhatsApp【英文标题】:How to open WhatsApp using an Intent in your Android App 【发布时间】:2016-11-20 05:02:18 【问题描述】:我想要一个 Intent 将您直接控制到 WhatsApp。所以当用户点击按钮的那一刻,意图应该把你带到 WhatsApp。 这是我在遵循一些指南后编写的代码,但它不起作用
buttonWhatsapp.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
// Performs action on click
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(Intent.createChooser(sendIntent, ""));
startActivity(sendIntent);
//opens the portfolio details class
);
【问题讨论】:
【参考方案1】:使用 2018 api:
String url = "https://api.whatsapp.com/send?phone="+number;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
【讨论】:
它对我有用。想提一下,我需要以+00 0000000000的格式发送号码 如果设备上没有安装 WhatsApp,它将打开一个网络浏览器来处理这个问题。不错! 我已经在移动浏览器中使用我的号码打开了这个 URL。我现在有自己的聊天记录,这会让非 IT 人员惊叹不已? 这样更好,因为不需要在App上安装Whatsapp 添加message
你必须使用"https://api.whatsapp.com/send?phone=$phoneNumber"+"&text=" + URLEncoder.encode(message, "UTF-8")
【参考方案2】:
这段代码对我有用
String contact = "+00 9876543210"; // use country code with your phone number
String url = "https://api.whatsapp.com/send?phone=" + contact;
try
PackageManager pm = context.getPackageManager();
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
catch (PackageManager.NameNotFoundException e)
Toast.makeText(MainActivity.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
e.printStackTrace();
【讨论】:
【参考方案3】:这在 2021 年完美运行
简短形式的扩展:
numero = 电话号码
mensaje= 要发送的消息
private void openWhatsApp(String numero,String mensaje)
try
PackageManager packageManager = getActivity().getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
String url = "https://api.whatsapp.com/send?phone="+ numero +"&text=" + URLEncoder.encode(mensaje, "UTF-8");
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null)
startActivity(i);
else
KToast.errorToast(getActivity(), getString(R.string.no_whatsapp), Gravity.BOTTOM, KToast.LENGTH_SHORT);
catch(Exception e)
Log.e("ERROR WHATSAPP",e.toString());
KToast.errorToast(getActivity(), getString(R.string.no_whatsapp), Gravity.BOTTOM, KToast.LENGTH_SHORT);
希望,这会有所帮助!
【讨论】:
【参考方案4】:btnWhatsapp.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View view)
startSupportChat ();
);
private void startSupportChat()
try
String headerReceiver = "";// Replace with your message.
String bodyMessageFormal = "";// Replace with your message.
String whatsappContain = headerReceiver + bodyMessageFormal;
String trimToNumner = "+910000000000"; //10 digit number
Intent intent = new Intent ( Intent.ACTION_VIEW );
intent.setData ( Uri.parse ( "https://wa.me/" + trimToNumner + "/?text=" + "" ) );
startActivity ( intent );
catch (Exception e)
e.printStackTrace ();
【讨论】:
【参考方案5】:我知道的最简单的方法是调用以下方法(使用字符串变量(消息)输入要通过 WhatAapp 发送的文本):
private void sendWhatsapp(String message)
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
if (sendIntent.resolveActivity(getPackageManager()) != null)
startActivity(sendIntent);
希望对你有帮助。
【讨论】:
如何在此处添加电话号码?【参考方案6】:嘿,这个 sn-p 来自官方 whatsapp 网站
https://www.whatsapp.com/faq/android/28000012
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);
【讨论】:
是的,我知道这就是我先写的,但它不起作用【参考方案7】:查看此方法
private void openWhatsApp(String smsNumber)
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hi, This is " + PreferenceManager.get(this, Constants.USERNAME));
sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
sendIntent.setPackage("com.whatsapp");
if (sendIntent.resolveActivity(getPackageManager()) == null)
Toast.makeText(this, "Error/n", Toast.LENGTH_SHORT).show();
return;
startActivity(sendIntent);
【讨论】:
【参考方案8】:? 只是一个桃色的答案
public static void setClickToChat(View v,String toNumber)
String url = "https://api.whatsapp.com/send?phone=" + toNumber;
try
PackageManager pm = v.getContext().getPackageManager();
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
v.getContext().startActivity(i);
catch (PackageManager.NameNotFoundException e)
v.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
【讨论】:
【参考方案9】:在 Kotlin 中,你会这样做。
打开特定用户的 WhatsApp 号码并发送输入消息
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(
"https://api.whatsapp.com/send?phone=Phone Number&text=Message to send"
)
)
)
【讨论】:
【参考方案10】: PackageManager pm = getActivity().getPackageManager();
try
// Raise exception if whatsapp doesn't exist
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, "YOUR TEXT");
startActivity(waIntent);
catch (PackageManager.NameNotFoundException e)
Toast.makeText(MainActivity.activity, "Please install whatsapp app", Toast.LENGTH_SHORT)
.show();
【讨论】:
是的,我现在就试试 @Ali Gürelli 表示无法解析符号活动 现在出现错误,无法解析getActivity()方法【参考方案11】:我在这里向您展示如何共享文本和图像, 对于共享文本,您可以使用这些代码,
private void shareTextUrl()
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, "http://www.codeofaninja.com");
startActivity(Intent.createChooser(share, "Share link!"));
现在如果你想分享图片,那么你可以使用这些代码,
private void shareImage()
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
【讨论】:
【参考方案12】:这是几天前的作品
private void openWhatsApp(String number)
try
number = number.replace(" ", "").replace("+", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(number)+"@s.whatsapp.net");
// getApplication().startActivity(sendIntent);
startActivity(Intent.createChooser(sendIntent, "Compartir en")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
catch(Exception e)
Log.e("WS", "ERROR_OPEN_MESSANGER"+e.toString());
【讨论】:
jid 是干什么用的?【参考方案13】:要同时处理business whatsapp和normal whatsapp,需要使用url scheme intent,因为使用包“com.whatsapp”的常规方法仅适用于普通的whatsapp。
这是处理普通和商业whatsapp的代码示例:
try
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("whatsapp://send?text=The text message goes here");
context.startActivity(i);
catch (Exception e)
Toast.makeText(context, "Whatsapp not installed!", Toast.LENGTH_LONG).show();
如果安装了两个whatsapp,并且只安装了其中一个,则将打开一个选择器。将启动特定版本。
【讨论】:
【参考方案14】:这个解决方案对我有用 :)
val url = "https://wa.me/WHATSAPP_NUMBER"
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(url)
startActivity(i)
【讨论】:
【参考方案15】:这段代码对我有用。
public void openWhatsapp(View view)
String message = mMessOpenWhatEdit.getText().toString(); // take message from the user
// create an Intent to send data to the whatsapp
Intent intent = new Intent(Intent.ACTION_VIEW); // setting action
// setting data url, if we not catch the exception then it shows an error
try
String url = "https://api.whatsapp.com/send?phone=+91 0000000000" + "&text=" + URLEncoder.encode(message, "UTF-8");
intent.setData(Uri.parse(url));
startActivity(intent);
catch(UnsupportedEncodingException e)
Log.d("notSupport", "thrown by encoder");
【讨论】:
openWhatsapp 是一个 onclick 方法(我的意思是它在点击 ui 中的按钮后调用)以上是关于如何在您的 Android 应用程序中使用 Intent 打开 WhatsApp的主要内容,如果未能解决你的问题,请参考以下文章
C#:ModernHttpClient,您在您的应用程序中引用便携式版本 - 您需要引用平台(iOS/Android)版本
错误尝试通过 callign firebase.app() 使用未安装在您的 Android 项目上的 firebase 模块