Vous êtes sur la page 1sur 3

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

Send text to specific contact (whatsapp) [duplicate]


android

android-intent

contact

whatsapp

This question already has an answer here:


Sending message through WhatsApp 10 answers

I wanted to know how I can send text to a specific whatsapp contact. I found some code to view a specific contact, but not to send data.
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
new String[] { id }, null);
c.moveToFirst();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
startActivity(i);
c.close();

This works fine for viewing a whatsapp-contact, but how can I add some text now? Or didn't the Whatsapp-developer implement such kind of an api?

share

improve this question


Manu
5,188 9 19 42

Asked
Sep 29 '13 at 18:24

Edited
Jun 16 '14 at 8:32

marked as duplicate by flx, Paresh Mayani, Leri, Laurent S., Ronak Mehta Feb 28 '14 at 12:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Duplciate of stackoverflow.com/questions/15462874/ rds Oct 6 '13 at 17:09

No, in your link they use a chooser, which I don't want to use. I want to send text directly to a contact Manu Oct 6 '13 at 18:10

He wants to open the chat history, while I want to send text. This is very different. Manu Oct 7 '13 at 7:53

@manu ..hey manu i have used your code for send message from my application to whatsup without open whatsup but i have error of getSherlockActivity() .I am new in android so i
dont understand what is getsherlockactivity() Montu Nov 16 '13 at 11:32

@Manu are you solved this problem ? BeingMIAkashs Dec 18 '13 at 7:18
show 1 more comment

1 Answer

34
+50

Order By

Votes

I think the answer is a mix of your question and this answer here: http://stackoverflow.com/a/15931345/734687 So I would try the following
code:
1. change ACTION_VIEW to ACTION_SENDTO
2. set the Uri as you did
3. set the package to whatsapp
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.setType("text/plain");
i.setPackage("com.whatsapp");
// so that only Whatsapp reacts and not the chooser
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
startActivity(i);

I looked into the Whatsapp manifest and saw that ACTION_SEND is registered to the activity ContactPicker, so that will not help you. However
ACTION_SENDTO is registered to the activity com.whatsapp.Conversation which sounds more adequate for your problem.
Whatsapp can work as a replacement for sending SMS, so it should work like SMS. When you do not specify the desired application (via
setPackage) Android displays the application picker. Thererfor you should just look at the code for sending SMS via intent and then provide the
additional package information.

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);

First try just to replace the intent ACTION_SEND to ACTION_SENDTO . If this does not work than provide the additional extra sms_body. If this does
not work than try to change the uri.
Update I tried to solve this myself and was not able to find a solution. Whatsapp is opening the chat history, but doesn't take the text and send
it. It seems that this functionality is just not implemented.

share

improve this answer


ChrLipp
10.7k 4 47 88

Answered
Oct 4 '13 at 10:36

Edited
Nov 15 '14 at 11:43

Only opens the contact, but no text passed to Whatsapp... Seems like Whatsapp doesn't recognise those extras (or doesn't want). Manu Oct 4 '13 at 16:39
Thank you, but now it shows the chooser... seems like WhatsApp blocked this way of sending text... Manu Oct 5 '13 at 7:45
It opens the chat history like all the other codes. Manu Oct 5 '13 at 13:09
Did you try all three variants? I will try to code it tomorrow. ChrLipp Oct 5 '13 at 13:38
Seems like Whatsapp blocked this way... Thank you for your help Manu Oct 10 '13 at 7:00
show 7 more comments
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Vous aimerez peut-être aussi