发送短信到特定的联系人(WhatsApp的) [英] Send text to specific contact (whatsapp)
问题描述
我想知道我可以发短信到特定的WhatsApp的接触。我发现了一些code,以查看特定的联系人,但不发送数据。
光标C = getContentResolver()查询(ContactsContract.Data.CONTENT_URI,
新的String [] {} ContactsContract.Contacts.Data._ID,ContactsContract.Data.DATA1 +=?,
新的String [] {ID},NULL);
c.moveToFirst();
意图I =新的意图(Intent.ACTION_VIEW,Uri.parse(内容://com.android.contacts/data/+ c.getString(0)));
startActivity(ⅰ);
c.close();
这工作正常查看WhatsApp的接触,但我怎么能现在添加一些文字?或者未在WhatsApp的开发者实现这类的API的?
我想答案是你的问题的结构和这个答案在这里:的 http://stackoverflow.com/a/15931345/734687 所以我想尝试以下code:
- 变化ACTION_VIEW到ACTION_SENDTO
- 将开放的我们像你一样
- 将包WhatsApp的
意向书我=新的意图(Intent.ACTION_SENDTO,Uri.parse(内容://com.android.contacts/数据/+ c.getString(0)));
i.setType(text / plain的);
i.setPackage(com.whatsapp); //这样只有WhatsApp的反应,而不是选择器
i.putExtra(Intent.EXTRA_SUBJECT,主题);
i.putExtra(Intent.EXTRA_TEXT,我的身体。);
startActivity(ⅰ);
我看着到WhatsApp的清单,看见ACTION_SEND注册到活动 ContactPicker
,这样就不会帮你。然而ACTION_SENDTO注册到活动 com.whatsapp.Conversation
这听起来更好地满足您的问题。
WhatsApp的可作为替代发送短信的工作,所以它应该工作一样的短信。如果没有指定所需的应用程序(通过 setPackage
)的Android显示应用程序管理器。 Thererfor你应该看看code通过故意发送短信,然后提供额外的软件包的信息。
开放的URI = Uri.parse(smsto:+ smsNumber);
意图I =新的意图(Intent.ACTION_SENDTO,URI);
i.putExtra(sms_body,smsText);
i.setPackage(com.whatsapp);
startActivity(ⅰ);
第一次尝试,以取代意图 ACTION_SEND
到 ACTION_SENDTO
。如果这样做的工作比提供的附加额外的 sms_body
。如果这样做的工作比试图改变URI。
更新 我试图解决这个我自己,没能找到解决的办法。 WhatsApp的是开放的聊天记录,但不采取文字和发送。看来,这个功能只是没有实现。
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?
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:
- change ACTION_VIEW to ACTION_SENDTO
- set the Uri as you did
- 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.
这篇关于发送短信到特定的联系人(WhatsApp的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!