Text not passing to Whatsapp application using Intent - android

I am trying to launch whatsapp app from my app and passing some text to a particular number. Whatsapp App is launching and but text is not able to pass.
String smsNumber = "98*******3";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, textWithClickableLink);
i.setPackage("com.whatsapp");
mContext.startActivity(i);
Is there anything missing in my code?
Please help me on this.
Thanks in advance!

Try this one:
Uri uri = Uri.parse("smsto:" + mobileno);
Intent whatsappIntent = new Intent(Intent.ACTION_SENDTO, uri);
whatsappIntent.setPackage("com.whatsapp");
String str = "https://play.google.com/store/apps/details?id=com.example.activity&hl=en";
whatsappIntent.putExtra("sms_body", str);
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
// Whatsapp have not been installed
}

you can try this method, it should work. I have tested
private void shareWhatsApp() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "text to share");
//if you have any images to share sue this, uri is the uri of image
//shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
//shareIntent.setType("image/*"); for image sharing
shareIntent.setType("text/plain");
shareIntent.setPackage("com.whatsapp");
startActivity(shareIntent);
}
This will launch WhatsApp application and you have to manually select the contacts and send. since they dont provide any SDK we cant send automatically.

I tried this code. It makes my app to open list of all app by which I can send messages. After selecting any app now I am able to send the message that I am attaching with intent.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "From Stockal App");
intent.putExtra(Intent.EXTRA_TEXT, text);
mContext.startActivity(intent);
Thank you all for your response.

Related

Send a text message from my android application to Whatsapp to specific Contact

I'm trying to send a text message from my android application to Whatsapp to specific Contact. when I'm using below codes, I am succeed either to send message and have to pickup contact manually, or If Specific number chat window opens, but message is blank. So is it possible to do both with one intent ? Here is my code:
I can share message to WhatsApp, but contact i have to choose manually:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.setPackage("com.whatsapp");
i.putExtra(Intent.EXTRA_TEXT, "Hello World");
try {
activity.startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
Specific number in wats app window opens, but message is blank:
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "smsText");
i.setPackage("com.whatsapp");
activity.startActivity(i);
Below code will helps you :
String strMessageToShare=YourEditText.getText().tostring();
final Intent myIntent = new Intent(
android.content.Intent.ACTION_SEND_MULTIPLE);
myIntent.setType("text/plain");
myIntent.putExtra(android.content.Intent.EXTRA_TEXT,
new String[]{strMessageToShare});
YourActivity.this.startActivityForResult(Intent
.createChooser(myIntent, "Sharing message..."), 1);
This is what works for me.
The parameter 'body' gets not read by the whatsapp app, use 'Intent.EXTRA_TEXT' instead.
By setting the 'phoneNumber' you specify the contact to open in whatsapp.
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("smsto:" + "" + phoneNumber + "?body=" + encodedMessage));
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);

Share data with WhatsApp

I'm wondering if I can send text data from my Android application to WhatsApp without showing the contacts list of WhatsApp, I just need to set the message and the mobile number internally in my Android code?
You can try something like this:
public void sendWhatsappMessageToMsisdn(String contactNumber)
{
Uri uri = Uri.parse("smsto:" + contactNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, "This is the message you want to send");
startActivity(Intent.createChooser(intent, ""));
}

Can we share image/text to whatsapp to specific contact number in android

In my application i need to share image/text to whatsapp. Sharing images or text is working fine. But my requirement is sharing images/text to specific recipant. For that i have mobile number. So before sharing, first of all that number is having whatsapp or not?. Then if the number having whatsapp, then bydefault select that specific number. If the number is not having whatsapp then simply redirect to whatspp. then they select the recipant and share to that recipant.
For sharing i am using the following code. This code is working fine for sharing.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
if (images.size() > 0) {
Log.e("count==", "val## " + images.size());
shareIntent
.putParcelableArrayListExtra(Intent.EXTRA_STREAM, images);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, title);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
shareIntent.putExtra(Intent.EXTRA_TEXT, title);
shareIntent.setType("text/plain");
}
shareIntent.setPackage("com.whatsapp");
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ManageOnlineBuyLeadActivity.this,
"Whatsapp have not been installed.", Toast.LENGTH_SHORT)
.show();
}
And for sharing particular recipant i have changed the follwing lines
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
to
Uri mUri = Uri.parse("smsto:+number");
Intent shareIntent = new Intent(Intent.ACTION_SENDTO, mUri);
But it is not working. For this i googled alot. So please guide me how to do this. Is it possible to share particular recipant or not?
Thank you all..
The easiest way I know is by calling the following method (Use the String variable message to input the text you want to send via 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);
}
}
I have a solution to send text only to a specific recipient. For sending an image to a specific recipient I am also using this.
String smsNumber = "Your specific contact No. here! ";
String msg = "Your message here!";
Uri uri = Uri.parse("http://api.whatsapp.com/send?phone="+smsNumber +"&text="+msg);
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
} catch(Exception e) {
Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show();
}

Send text and image to specific contact (whatsapp)

I wanted to know how I can send text and image to a specific whatsapp contact. I found some code to view a specific contact, but not to send data.
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);
i.setPackage("com.whatsapp");
startActivity(i);
But that code just opening the chat history, but doesn't take the text and image and send it.
I also try the below code for send image and text via whatsApp but that ask for choose the contact for send
Intent shareIntent = new Intent();
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, sendString);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent. setPackage ("com. whatsapp");
startActivity(shareIntent);
If that functionality is possible then please give me a suggestion for this.
Solution :
Intent sendIntent = new Intent("android.intent.action.SEND");
File f=new File("path to the file");
Uri uri = Uri.fromFile(f);
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
sendIntent.setType("image");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("919xxxxxxxxx")+"#s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
startActivity(sendIntent);
Refer to my answer at for detailed explanation.
https://stackoverflow.com/a/41805567/3989718

Sending image via WhatsApp to specific recipient (Android)

I'm sharing my images via WhatsApp - but I have to choose the recipient.
Here is my code:
public static void shareImage(Context context,String path, String text, String otherAppPackage){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.setPackage("com.whatsapp");
share.putExtra(android.content.Intent.EXTRA_SUBJECT, getSubject(context));
if (text!=null){
share.putExtra(Intent.EXTRA_TEXT,text);
}
if (path!=null){
share.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(path)));
}
context.startActivity(Intent.createChooser(share, context.getString(R.string.share_via)));
}
I wold like to share with someone directly. Does some of you know how can I do this.
Thanks
You can use Intent.ACTION_SENDTO, but message is not copied to clipboard then:
Uri uri = Uri.parse("smsto:+123456789");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.setPackage("com.whatsapp");
it.putExtra("sms_body", "The SMS text");
it.putExtra("chat",true);
startActivity(it);

Categories

Resources