如何在Android应用程序中使用Intent打开WhatsApp
我想让你直接控制WhatsApp。因此,当用户点击按钮时,其意图应该是将您带到WhatsApp。 这是我在遵循一些指导原则后编写的代码,但它不起作用如何在Android应用程序中使用Intent打开WhatsApp,android,android-intent,comments,onclicklistener,whatsapp,Android,Android Intent,Comments,Onclicklistener,Whatsapp,我想让你直接控制WhatsApp。因此,当用户点击按钮时,其意图应该是将您带到WhatsApp。 这是我在遵循一些指导原则后编写的代码,但它不起作用 buttonWhatsapp.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Performs action on click Intent sendIntent = ne
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
}
});
我在这里向您展示如何共享文本和图像, 对于共享文本,您可以使用以下代码
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!"));
}
现在,如果您想共享图像,那么您可以使用这些代码
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!"));
}
嘿,这个片段来自whatsapp官方网站
我知道的最简单的方法是调用以下方法(使用字符串变量(message)输入要通过WhatAapp发送的文本):
我希望这对您有所帮助。使用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);
这在2021年非常有效
简短形式的扩展:
数字电话
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);
}
}
希望,这有帮助
这个代码对我有用
检查此方法
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);
}
这是几天前的工作
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());
}
}
要处理业务whatsapp和普通whatsapp,需要使用url方案意图,因为使用包“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,并且只安装了其中一个,则会打开一个选择器,该特定版本将被启动。在Kotlin中,您将这样做
打开特定用户WhatsApp号码并发送键入的消息
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(
"https://api.whatsapp.com/send?phone=Phone Number&text=Message to send"
)
)
)
是的,我现在将尝试@Ali Gürelli它说它无法解析符号活动它现在有一个错误,它无法解析方法getActivity(),是的,我知道这是我首先写的,但它对我不起作用。我想说的是,我需要以+00 0000000000的格式发送号码,如果设备上没有安装WhatsApp,它将打开一个web浏览器来处理这个问题。美好的我已在手机浏览器中使用我的号码打开此URL。我现在有自己的聊天室,这会让非IT人员去哇,这更好,因为不需要在应用程序上安装Whatsapp来添加
消息
,您必须使用”https://api.whatsapp.com/send?phone=$phoneNumber“+”&text=“+urlcoder.encode(消息,“UTF-8”)
您将消息放在了什么地方???jid的用途是什么?
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());
}
}
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();
}
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(
"https://api.whatsapp.com/send?phone=Phone Number&text=Message to send"
)
)
)