I want to create an IntentChooser who offer to share text only via SMS or WhatsApp.
Here is my code to share via WhatsApp:
Intent localIntent = new Intent(Intent.ACTION_SEND);
localIntent.setType("text/plain");
localIntent.setPackage("com.whatsapp");
if (localIntent != null) {
localIntent.putExtra(Intent.EXTRA_TEXT, "Hi there! I'm using this app");
startActivity(Intent.createChooser(localIntent, "Hi there! I'm using this app");
}
I need to add to this also sharing with SMS. How can I do it?
use this for Whatsapp.
try {
startActivity(new Intent(Intent.ACTION_SEND).setType("text/plain").setPackage("com.whatsapp").putExtra(Intent.EXTRA_TEXT, Message));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}
and this for SMS .
String number = "12346556"; // The number on which you want to send SMS
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));
OR
Possibly Duplicate of Multiple IntentChooser
Related
I would like to send an email to given email address with some default instructions. I know that I can achieve it for sms by using twilio but don't know how to do this via mail..
You can directly use following to send mail with body:
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String\[\]{ "serveroverloadofficial#gmail.com"});
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hello There");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Add Message here");
intent.setType("message/rfc822");
try {
startActivity(Intent.createChooser(intent,
"Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(),
"No email clients installed.",
Toast.LENGTH_SHORT).show();
}
}
});
Use Mailgun.com to send email by Using their API.
I'm developing an application that can send message through whatsapp. I have implemented it by following this answer but it only works for contacts that on my contact list. It keeps error when i try to send message to contacts that are not in my contact list although the user already registered on whatsapp using that number (contact). Here is the message from whatsapp:
here is my code:
try {
Uri uri = Uri.parse("smsto: " + smsNumber);
//Timber.e("smsNumber %s", uri.toString());
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
} catch (Exception e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
}
is there any solutions ?
you can send via WhatsApp API even if he is not in your contact
String whatsappUrl = String.format("https://api.whatsapp.com/send?phone=%s&text%s",PhoneNumber,msg);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(whatsappUrl));
browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
p_context.startActivity(browserIntent);
I am building an android app. My app needs to send the customer's(who is giving purchase order) phone number to a specific mobile number via either whatsapp or sms. But I also want to restrict all other apps from my share via list other than these two.
I know how to use sharing Intent but don't know how to restrict all other apps from that drop down list.
To restric all the other app you need to provide the package name of the app you wanted to open e.g.
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.whatsapp");
this is the package name for whats app.
you can easily get the packege name from setting->apps-> your App
For more information click here
Try this for whatsapp
public void shareViaWhatsapp() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
For SMS
public void sendSMS(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(getActivity()); // Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
// any app that support this intent.
{
sendIntent.setPackage(defaultSmsPackageName);
}
startActivity(sendIntent);
}
else // For early versions, do what worked for you before.
{
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("sms_body","This is my text to send.");
startActivity(smsIntent);
}
}
Above answer is sufficient for your question.But for other coders this may help :)
For whatsapp :
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, textToSend);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
Toast.makeText(ShareActivity.this,"Could not launch WhatsApp.",Toast.LENGTH_SHORT).show();
}
For Message :
try {
Intent sendIntentmsg = new Intent(Intent.ACTION_VIEW);
sendIntentmsg.setData(Uri.parse("sms:"));
sendIntentmsg.putExtra("sms_body", textToSend);
startActivity(sendIntentmsg);
} catch (Exception e) {
Toast.makeText(ShareActivity.this,"Could not launch Messaging App now.",Toast.LENGTH_SHORT).show();
}
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();
}
I read this question to know how to send email in Android using ACTION_SEND: Sending email from android app
But problem is: I want to send email directly using Gmail, i don't want to show the action-list and choose Gmail again.
Can I do it?
How to open Gmail Compose when a button is clicked in Android App? the second answer of beekeeper. Also duplicate of how to direct open Gmail mail composer in android? which has the same answer.
Try this:
final Intent intent = new Intent (android.content.Intent.ACTION_SEND);
intent.setType ("text/plain");
List<ResolveInfo> resInfo = getPackageManager ().queryIntentActivities (intent, 0);
if (!resInfo.isEmpty ()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase ().contains ("android.gm") || info.activityInfo.name.toLowerCase ().contains ("android.gm")) {
intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, TextKonnex);
intent.putExtra(android.content.Intent.EXTRA_TEXT, message);
intent.setPackage (info.activityInfo.packageName);
try {
startActivity (android.content.Intent.createChooser (intent,"Sending..."));
Toast.makeText(Main4Activity.this, "Sending an email to your friend! ", Toast.LENGTH_LONG).show();
} catch (ActivityNotFoundException e) {
Toast.makeText(Main4Activity.this, "Error! Try whith other email address! ", Toast.LENGTH_LONG).show();
}
}
}