可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
I think the answer is a mix of your question and this answer here: https://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.
回答2:
I have done it!
private void openWhatsApp() {
String smsNumber = \"7****\"; // E164 format without \'+\' sign
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(\"text/plain\");
sendIntent.putExtra(Intent.EXTRA_TEXT, \"This is my text to send.\");
sendIntent.putExtra(\"jid\", smsNumber + \"@s.whatsapp.net\"); //phone number without \"+\" prefix
sendIntent.setPackage(\"com.whatsapp\");
if (intent.resolveActivity(getActivity().getPackageManager()) == null) {
Toast.makeText(this, \"Error/n\" + e.toString(), Toast.LENGTH_SHORT).show();
return;
}
startActivity(sendIntent);
}
回答3:
I found the right way to do this and is just simple after you read this article: http://howdygeeks.com/send-whatsapp-message-unsaved-number-android/
phone and message are both String.
Source code:
PackageManager packageManager = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
try {
String url = \"https://api.whatsapp.com/send?phone=\"+ phone +\"&text=\" + URLEncoder.encode(message, \"UTF-8\");
i.setPackage(\"com.whatsapp\");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
context.startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
Enjoy!
回答4:
This approach works with WhatsApp Business app as well!
Change package name as sendIntent.setPackage(\"com.whatsapp.w4b\"); for WhatsApp Business.
Great hack Rishabh, thanks a lot, I was looking for this solution since last 3 years.
As per the Rishabh Maurya\'s answer above, I have implemented this code which is working fine for both text and image sharing on WhatsApp.
Note that in both the cases it opens a whatsapp conversation (if toNumber exists in users whatsapp contact list), but user have to click send button to complete the action. That means it helps in skipping contact selection step.
For text messages
String toNumber = \"+91 98765 43210\"; // contains spaces.
toNumber = toNumber.replace(\"+\", \"\").replace(\" \", \"\");
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
sendIntent.putExtra(\"jid\", toNumber + \"@s.whatsapp.net\");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage(\"com.whatsapp\");
sendIntent.setType(\"text/plain\");
startActivity(sendIntent);
For sharing images
String toNumber = \"+91 98765 43210\"; // contains spaces.
toNumber = toNumber.replace(\"+\", \"\").replace(\" \", \"\");
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
sendIntent.putExtra(\"jid\", toNumber + \"@s.whatsapp.net\");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage(\"com.whatsapp\");
sendIntent.setType(\"image/png\");
context.startActivity(sendIntent);
Enjoy WhatsApping!
回答5:
It lets you open WhatsApp conversation screen for that specific user you are trying to communicate with:
private void openWhatsApp() {
String smsNumber = \"91XXXXXXXX20\";
boolean isWhatsappInstalled = whatsappInstalledOrNot(\"com.whatsapp\");
if (isWhatsappInstalled) {
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
sendIntent.setComponent(new ComponentName(\"com.whatsapp\", \"com.whatsapp.Conversation\"));
sendIntent.putExtra(\"jid\", PhoneNumberUtils.stripSeparators(smsNumber) + \"@s.whatsapp.net\");//phone number without \"+\" prefix
startActivity(sendIntent);
} else {
Uri uri = Uri.parse(\"market://details?id=com.whatsapp\");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
Toast.makeText(this, \"WhatsApp not Installed\",
Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
private boolean whatsappInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
回答6:
Check out my answer : https://stackoverflow.com/a/40285262/5879376
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
sendIntent.setComponent(new ComponentName(\"com.whatsapp\",\"com.whatsapp.Conversation\"));
sendIntent.putExtra(\"jid\", PhoneNumberUtils.stripSeparators(\"YOUR_PHONE_NUMBER\")+\"@s.whatsapp.net\");//phone number without \"+\" prefix
startActivity(sendIntent);
回答7:
This will first search for the specified contact and then open a chat window.
And if WhatsApp is not installed then try-catch block handle this.
String digits = \"\\\\d+\";
Sring mob_num = 987654321;
if (mob_num.matches(digits))
{
try {
/linking for whatsapp
Uri uri = Uri.parse(\"whatsapp://send?phone=+91\" + mob_num);
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
}
catch (ActivityNotFoundException e){
e.printStackTrace();
Toast.makeText(this, \"WhatsApp not installed.\", Toast.LENGTH_SHORT).show();
}
}
回答8:
Whatsapp new feature is arrived try these
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
sendIntent.setAction(Intent.ACTION_VIEW);
sendIntent.setPackage(\"com.whatsapp\");
String url = \"https://api.whatsapp.com/send?phone=\" + \"Phone with international format\" + \"&text=\" + \"your message\";
sendIntent.setData(Uri.parse(url));
startActivity(sendIntent);
Credits
See this documentation
回答9:
Check this answer. Here your number start with \"91**********\".
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType(\"text/plain\");
sendIntent.putExtra(Intent.EXTRA_TEXT, \"This is my text to send.\"); sendIntent.putExtra(\"jid\",PhoneNumberUtils.stripSeparators(\"91**********\") + \"@s.whatsapp.net\");
sendIntent.setPackage(\"com.whatsapp\");
startActivity(sendIntent);
回答10:
try this, worked for me ! . Just use intent
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(whatsappUrl()));
startActivity(intent);
Build whatsapp url. add country code in whatsapp phone number https://countrycode.org/
public static String whatsappUrl(){
final String BASE_URL = \"https://api.whatsapp.com/\";
final String WHATSAPP_PHONE_NUMBER = \"628123232323\"; //\'62\' is country code for Indonesia
final String PARAM_PHONE_NUMBER = \"phone\";
final String PARAM_TEXT = \"text\";
final String TEXT_VALUE = \"Hello, How are you ?\";
String newUrl = BASE_URL + \"send\";
Uri builtUri = Uri.parse(newUrl).buildUpon()
.appendQueryParameter(PARAM_PHONE_NUMBER, WHATSAPP_PHONE_NUMBER)
.appendQueryParameter(PARAM_TEXT, TEXT_VALUE)
.build();
return buildUrl(builtUri).toString();
}
public static URL buildUrl(Uri myUri){
URL finalUrl = null;
try {
finalUrl = new URL(myUri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
return finalUrl;
}
回答11:
This will first search for the specified contact and then open a chat window.
Note: phone_number and str are variables.
Uri mUri = Uri.parse(\"https://api.whatsapp.com/send?
phone=\" + phone_no + \"&text=\" + str);
Intent mIntent = new Intent(\"android.intent.action.VIEW\", mUri);
mIntent.setPackage(\"com.whatsapp\");
startActivity(mIntent);
回答12:
private void openWhatsApp() {
//without \'+\'
try {
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
//sendIntent.setComponent(new ComponentName(\"com.whatsapp\", \"com.whatsapp.Conversation\"));
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType(\"text/plain\");
sendIntent.putExtra(\"jid\",whatsappId);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setPackage(\"com.whatsapp\");
startActivity(sendIntent);
} catch(Exception e) {
Toast.makeText(this, \"Error/n\" + e.toString(), Toast.LENGTH_SHORT).show();
Log.e(\"Error\",e+\"\") ; }
}
回答13:
This is the shortest way
String mPhoneNumber = \"+972505555555\";
mPhoneNumber = mPhoneNumber.replaceAll(\"+\", \"\").replaceAll(\" \", \"\").replaceAll(\"-\",\"\");
String mMessage = \"Hello world\";
String mSendToWhatsApp = \"https://wa.me/\" + mPhoneNumber + \"?text=\"+mMessage;
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(
mSendToWhatsApp
)));
See also the documentation of WhatsApp
回答14:
This is now possible through the WhatsApp Business API. Only businesses may apply to use it. This is the only way to directly send messages to phone numbers, without any human interaction.
Sending normal messages is free. It looks like you need to host a MySQL database and the WhatsApp Business Client on your server.
回答15:
String toNumber = \"+92307 8401217\"; // contains spaces.
toNumber = toNumber.replace(\"+\", \"\").replace(\" \", \"\");
Intent sendIntent = new Intent(\"android.intent.action.MAIN\");
sendIntent.putExtra(\"jid\", toNumber + \"@s.whatsapp.net\");
sendIntent.putExtra(Intent.EXTRA_TEXT, \"message\");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage(\"com.whatsapp\");
sendIntent.setType(\"text/plain\");
startActivity(sendIntent);
回答16:
Bitmap bmp = null;
bmp = ((BitmapDrawable) tmpimg.getDrawable()).getBitmap();
Uri bmpUri = null;
try {
File file = new File(getBaseContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES), \"share_image_\" + System.currentTimeMillis() + \".jpg\");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
String toNumber = \"+919999999999\";
toNumber = toNumber.replace(\"+\", \"\").replace(\" \", \"\");
Intent shareIntent =new Intent(\"android.intent.action.MAIN\");
shareIntent.setAction(Intent.ACTION_SEND);
String ExtraText;
ExtraText = \"Share Text\";
shareIntent.putExtra(Intent.EXTRA_TEXT, ExtraText);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType(\"image/jpg\");
shareIntent.setPackage(\"com.whatsapp\");
shareIntent.putExtra(\"jid\", toNumber + \"@s.whatsapp.net\");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getBaseContext(), \"Sharing tools have not been installed.\", Toast.LENGTH_SHORT).show();
}
}