I am trying to send some message to whatsapp using the following code but it gives error android system has stopped but the intent is working fine. and the text is also added in the message. What could be the problem?
PackageManager pm = getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = " http://play.google.com/ ";
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
Found this answer
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "share text");
try {
startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Whatsapp not installed", Toast.LENGTH_SHORT).show();
}
Related
String url ="https://m.facebook.com/messages/read/?fbid=101631428274763";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
I want to directly send some messages. This way I am able to send message to fb page...but, not able to add some texts to text field. So, how can I send messages? I know that I can do that by intent.putExtra but, what the name would be?
Here is You sent a message direct to FB messenger.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "My message to send");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.facebook.orca");
try {
startActivity(sendIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.show(this, "Please Install Facebook Messenger");
}
Following source code worked properly for me
Uri uri = Uri.parse("fb-messenger://user/101631428274763");
Intent toMessenger= new Intent(Intent.ACTION_VIEW, uri);
toMessenger.putExtra(Intent.EXTRA_TEXT, "My message to send");
try {
startActivity(toMessenger);
}
catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
}
Tried out these following code snippets to send pdf, image and text. When used ACTION_VIEW the particular whatsapp chat is opening, text is getting sent but can't able to send the pdf or image. Please help with appropriate code snippet for the following problem I am facing.
The methods which I tried are as follows:
PackageManager pm = bill_layout.this.getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("application/pdf");
String text = "YOUR TEXT HERE";
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(bill_layout.this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
try {
String longUrl="https://firebasestorage.googleapis.com/v0/b/glossy-radio-280106.appspot.com/o/19c48c9d-3f83-4a8f-a296-fe0ea
Bitly bitly= Bit.ly("o_1gduh4caf3");
shorturl=bitly.shorten(longUrl);
String text= "shorturl";// Replace with your message.
String toNumber = "91xxxxxxxxxx"; // Replace with mobile phone number without +Sign or leading zeros, but with country code
//Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://api.whatsapp.com/send?phone="+toNumber +"&text="+ URLEncoder.encode(text, "UTF-8"))
"text/plain");
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,"okay");
intent.setPackage("com.whatsapp");
startActivity(intent); }
catch (Exception e){
e.printStackTrace();}
PackageManager pm1=getPackageManager();
try {
String toNumber = "91xxxxxxxxxx"; // Replace with mobile phone number without +Sign or leading zeros, but with country co
//Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”.
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:"+ toNumber));
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT,"test1");
sendIntent.putExtra("chat",true);
startActivity(sendIntent);}
catch (Exception e){
e.printStackTrace();
Toast.makeText(bill_layout.this,"it may be you dont have whats app",Toast.LENGTH_LONG).show();
}
Intent sendIntent = new Intent("android.intent.action.SEND");
File f=new File("path to the file");
Uri uri = Uri.parse("gs://glossy-radio-280106.appspot.com/19c48c9d-3f83-4a8f-a296-fe0eaf4488c4.pdf");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
sendIntent.setType("application/pdf");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
sendIntent.putExtra("jid","918824525121"+"#s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
startActivity(sendIntent);
PackageManager pm = bill_layout.this.getPackageManager();
String formattedNumber ="91xxxxxxxxxx";
try{
Intent sendIntent =new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
sendIntent.putExtra(Intent.EXTRA_TEXT,formattedNumber);
sendIntent.setType("text/plain");
sendIntent.putExtra("jid", formattedNumber +"#s.whatsapp.net");
sendIntent.setPackage("com.whatsapp");
bill_layout.this.startActivity(sendIntent);
}
catch(Exception e)
{
Toast.makeText(bill_layout.this,"Error/n"+ e.toString(),Toast.LENGTH_SHORT).show();
Try this, set you type i.e file, image or text.
Intent sendIntent = new Intent("android.intent.action.SEND");
File f=new File("Your file path");
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("91xxxxxxxxxx")+"#s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT,"Sample text");
startActivity(sendIntent);
I`m trying to send an email to multiple people without opening the chooser is that possible?
I tried to use a array but it gave me an error.
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("example#gmail.com") +
"?subject=" + Uri.encode("the subject") +
"&body=" + Uri.encode("the body of the message");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send Email..."))
Correct way to send email to multiple user is:
Kotlin:
private fun emailToMultipleUser() {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.setPackage("com.google.android.gm")
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("example#gmail.com","chand#gmail.com"))
intent.putExtra(Intent.EXTRA_SUBJECT, "email subject")
intent.putExtra(Intent.EXTRA_TEXT, "Hi All ...")
try {
startActivity(Intent.createChooser(intent, "send mail"))
} catch (ex: ActivityNotFoundException) {
Toast.makeText(this, "No mail app found!!!", Toast.LENGTH_SHORT)
} catch (ex: Exception) {
Toast.makeText(this, "Unexpected Error!!!", Toast.LENGTH_SHORT)
}
}
Java:
private void emailToMultipleUser() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType( "text/plain");
intent.setPackage("com.google.android.gm");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"example#gmail.com","chand#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "email subject");
intent.putExtra(Intent.EXTRA_TEXT, "Hi All ...");
try {
startActivity(Intent.createChooser(intent, "send mail"));
} catch (ActivityNotFoundException ex) {
Toast.makeText(this, "No mail app found!!!", Toast.LENGTH_SHORT);
} catch (Exception ex) {
Toast.makeText(this, "Unexpected Error!!!", Toast.LENGTH_SHORT);
}
}
I have a button in my app that opens the Whatsapp Intent.
After selecting contacts, the user will send a message to all selected contact.
I want to take all the contacts the user sent a message to and save it in the app.
How should I access the numbers?
There is any getPhoneNumber function after whats app intent is closed?
There is my function to send a message :
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
I try to send text message with whatsapp but this code fails every time.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, txtMessage.getText().toString());
startActivity(shareIntent);
Here is the solution for send the message to whatsapp from our application
public void onClickWhatsApp() {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Also see http://www.whatsapp.com/faq/en/android/28000012