I would like to know if it is possible to send a SMS from an Android tablet using the SMS intent? If this is not possible, what are my options?
I would like to know if it is possible to send a SMS from an Android tablet using the SMS intent?
There isn't really an "SMS" Intent. There are ACTION_SEND and ACTION_SENDTO Intent actions that could result in an SMS being sent.
With respect to "tablets", most devices with above-average screen sizes do not have telephony capability, and therefore cannot do anything with SMSes, let alone send them in response to startActivity() on some Intent.
what are my options?
If you absolutely have to be able to send SMS messages, add <uses-feature android:name="android.hardware.telephony"/> to your manifest, so your app will only be installed on devices that have telephony capability.
If you would like to send SMS messages if that is possible, but work around it if it is not possible, you will want to do three things:
Add <uses-feature android:name="android.hardware.telephony" android:required="false"/> to your manifest
Use PackageManager and hasSystemFeature() to see if you actually have telephony capability at runtime
For devices that have telephony capability, before you call startActivity() on your "SMS Intent", use PackageManager and queryIntentActivities() to see if there is anything on the device that will respond to that Intent, or wrap your startActivity() call in an exception handler to catch the
ActivityNotFoundException
String smsNumber = "your number here";
String smsText = "Your text";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
+ phoneNumber)));
Try this.
How to check if a tablet has sms service available:
Here the third solution which CommonsWare described in his answer as a method:
public static boolean hasSmsService(Context context)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:123456789"));
PackageManager pm = context.getPackageManager();
List<ResolveInfo> res = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if(res.size() > 0)
{
return true;
}
return false;
}
}
This is wok for you. try it.........
Method :
CAll on button click event.....
sendSMS("Any text",number,sms_string);
Now, declare this one out of oncreate();
public static void sendSMS(String status, String phoneNumber, String message) {
Log.e("", "Page : " + status + ", No : " + phoneNumber
+ ",Message Length: " + message.length() + ", Message : "
+ message);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
Related
This code was working, it opens the default SMS app and message appears in textbox , ready to enter number and send, But now this code doesn't work from Above marshmallow.
but now It does not work if the default app is Messaging
Uri uri = Uri.parse("smsto:" + mphoneno);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", message);
mActivity.startActivity(it);
try below method to open default messaging app
public void sendSMS() {
String number = "12346556"; // The number on which you want to send SMS
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));
}
I'm using following code for sending text message via Intent ( can not ask for permission so smsmanager is not an option)
//Code from this question
// <http://stackoverflow.com/questions/20079047/android-kitkat-4-4-hangouts-cannot-handle-sending-sms-intent>
private void sendsms(String toContact, String text){
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact)));
intent.putExtra("sms_body", text);
if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
{
intent.setPackage(defaultSmsPackageName);
}
}
else
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", toContact);
intent.putExtra("sms_body", text);
}
this.startActivity(intent);
}
and I'm calling this in a for loop :
for(int i = 0; i<4 ;i++) {
sendsms(phoneNo[i],smsBody[i]);
}
Now the problem is whenever user gets to this line, the void will be called 4 times, but user will only see the last message in the devices default messaging app ready to be sent, but to get to the other ones, user should press back on the device and if not, he/she would never see the other messages.
what I need to be done is using a method like startActivityForResult(); so each time user sends the message he would be redirected to my app, and then my app starts another activity for the next text message.
any idea?
Thanks in advance
First of all you need to create a String with your phones, for that you have to use "; " as separator, but take care with Samsung, in those devices you have to use ", ".
So your code has to be similar to this one:
private void sendSms(String phone1, String phone2){
String separator = "; "
if (Build.MANUFACTURER.toLowerCase().contains("samsung"))
separator = ", "
String phones = phone1 + separator + phone2...
Intent intentSms = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phones, null));
intentSms.putExtra("sms_body","your text") // Just if you want to add text
startActivity(intentSms);
}
I am facing a very strange issue. I am sending sms from my app using intent. But, two sms are sent at a time and user is charged for two. I had checked for any duplicates in my project but found nothing. I had gone some queries posted here related to same issue but they didn't helped me. Below, is my code to send sms:
String mMailSubject = "iTithe Android App";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Log.e("ITITHE SMS INTENT", "KITKAT");
String defaultSmsPackageName = Telephony.Sms
.getDefaultSmsPackage(MoreActivity.this);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, mMailSubject + "\n" + msg);
if (defaultSmsPackageName != null) {
sendIntent.setPackage(defaultSmsPackageName);
}
} else {
Log.e("ITITHE SMS INTENT", "OTHER PLATFORM");
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", mMailSubject + "\n" + msg);
}
startActivity(sendIntent);
The problem was not with code but with the length of message. The message length exceeded 160 characters so the SMS was sent in two parts.
Code for sending sms that worked perfectly until Android 4.3 (Jelly Bean) and stopped working since 4.4 (KitKat)
I'm just preparing the text message for the user, but they need to choose the number to send to.
The code I have used is:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
activity.startActivity(sendIntent);
Since it stopped working, I have also tried the ACTION_SEND and ACTION_SENDTO Neither worked, I also tried the sendIntent.setType("vnd.android-dir/mms-sms");, but again nothing worked.
I looked at several answers on Stack Overflow answer 1 and answer 2, but both answers aren't dealing with the requirements I have.
What I would like to do:
Send sms with sms app only, not by all apps that serves the send intent
Prepare the text for the user
Let the user choose the phone number to send the message to
For moderators:
It is not a duplicate questions, since the questions, doesn't ask the exact same thing, the need here is to send sms with no phone number, and none of the questions and answers dealt with that.
I attached a code that solve the problem by doing the following:
Check the OS version
In case that older version (prior to KitKat), use the old method
If new API, check the default sms package. if there is any, set it as the package, otherwise, let the user choose the sharing app.
Here is the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); //Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, smsText);
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);
}
activity.startActivity(sendIntent);
}
else //For early versions, do what worked for you before.
{
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
activity.startActivity(sendIntent);
}
This one should work on all android versions and all sms apps (including Hangouts).
public static boolean sendSms(Context context, String text, String number) {
return sendSms(context, text, Collections.singletonList(number));
}
public static boolean sendSms(Context context, String text, List<String> numbers) {
String numbersStr = TextUtils.join(",", numbers);
Uri uri = Uri.parse("sms:" + numbersStr);
Intent intent = new Intent();
intent.setData(uri);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.putExtra("sms_body", text);
intent.putExtra("address", numbersStr);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setAction(Intent.ACTION_SENDTO);
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
if(defaultSmsPackageName != null) {
intent.setPackage(defaultSmsPackageName);
}
} else {
intent.setAction(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
}
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}
Combining the proposed solutions, the following provides presetting the recipient and the text.
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact.toString())));
intent.putExtra("sms_body", text);
if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
{
intent.setPackage(defaultSmsPackageName);
}
}
else
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", toContact.toString());
intent.putExtra("sms_body", text);
}
activity.startActivity(intent);
The only problem remaining is that you end up in Hangouts (Nexus 5), and you might have to press "back" multiple times to effectively cancel the SMS.
In Kotlin following code works:
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity)
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.type = "text/plain"
sendIntent.putExtra("address", "sms:"+contactNumber)
sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_msg_body))
Timber.e("defaultSmsPackageName: "+defaultSmsPackageName)
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)
activity!!.startActivity(sendIntent)
}
My app integrates e-mail where the user can submit a bug report, feedback, etc. from the app directly. I'm using the application/octet-stream as the SetType for the Intent. When you go to submit the e-mail you get the content chooser and it shows various items from Evernote, Facebook, E-mail, etc.
How can I get this chooser to only show E-mail so as not to confuse the user with all these other items that fit the content chooser type?
Thank you.
To solve this issue simply follow the official documentation. The most important consideration are:
The flag is ACTION_SENDTO, and not ACTION_SEND.
The setData of method of the intent,
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
If you send an empty Extra, the if() at the end won't work and the app won't launch the email client.
This works for me. According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
https://developer.android.com/guide/components/intents-common.html#Email
I am presuming that you are using the ACTION_SEND Intent action, since you did not bother to actually state what you're using, but you agreed with #Aleadam's comment.
I'm using the application/octet-stream as the SetType for the Intent.
Nothing in that sentence limits things to email.
ACTION_SEND is a generic Intent action that can be supported by any application that wants to. All you do is indicate what data you are sharing and the MIME type of that data -- from there, it is up to the user to choose from available activities.
As #Jasoon indicates, you can try message/rfc822 as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.
If you specifically want to send something by email, integrate JavaMail into your app, or write an email forwarding script on your Web server and invoke it, or something. If you use ACTION_SEND, you are implicitly stating that it is what the user wants that matters, and you want the user to be able to send such-and-so data by whatever means the user chooses.
Just struggled with this problem while implementing a Magic Link feature, a chooser intent for all installed email apps:
Chooser Intent Screenshot
private void openEmailApp() {
List<Intent> emailAppLauncherIntents = new ArrayList<>();
//Intent that only email apps can handle:
Intent emailAppIntent = new Intent(Intent.ACTION_SENDTO);
emailAppIntent.setData(Uri.parse("mailto:"));
emailAppIntent.putExtra(Intent.EXTRA_EMAIL, "");
emailAppIntent.putExtra(Intent.EXTRA_SUBJECT, "");
PackageManager packageManager = getPackageManager();
//All installed apps that can handle email intent:
List<ResolveInfo> emailApps = packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_ALL);
for (ResolveInfo resolveInfo : emailApps) {
String packageName = resolveInfo.activityInfo.packageName;
Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
emailAppLauncherIntents.add(launchIntent);
}
//Create chooser
Intent chooserIntent = Intent.createChooser(new Intent(), "Select email app:");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toArray(new Parcelable[emailAppLauncherIntents.size()]));
startActivity(chooserIntent);
}
There is a way more generic to do that, working with any MIME type.
See this post: How to customize share intent in Android?
It is possible to limit the choices of an intent chooser to just a few options. The code in the answer to this question is a good example. In essence, you would have to create a List of LabeledIntents to provide to the intent chooser, that will then include it in its list. Note that this solution works not on exclusion (certain apps are excluded while the rest remain) but instead you have to pick which apps to display. Hope it helps!
It works on all devices. It will show only Email Apps
public static void shareViaMail(Activity activity, String title, String body, String filePath) {
Uri URI = Uri.parse("file://" + filePath);
final Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"contact#brightsociety.com"});
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
try {
activity.startActivity(emailIntent);
} catch (Exception e) {
((BaseActivity) activity).showToast("Gmail App is not installed");
e.printStackTrace();
}
}
Kotlin Answer
If you need to show only email apps and then you want to open only inbox (not open new email writing), you need to do A and B:
A) Add below code in your AndroidManifest.xml file for Android 11 because of package visibility update of Android 11 :
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
<intent>
<action android:name="android.intent.action.CHOOSER" />
</intent>
</queries>
B) Use below function to show email chooser:
// Show email app list.
fun showEmailAppList() {
// Email app list.
val emailAppLauncherIntents: MutableList<Intent?> = ArrayList()
// Create intent which can handle only by email apps.
val emailAppIntent = Intent(Intent.ACTION_SENDTO)
emailAppIntent.data = Uri.parse("mailto:")
// Find from all installed apps that can handle email intent and check version.
val emailApps = packageManager.queryIntentActivities(
emailAppIntent,
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) 0 else PackageManager.MATCH_ALL
)
// Collect email apps and put in intent list.
for (resolveInfo in emailApps) {
val packageName = resolveInfo.activityInfo.packageName
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
emailAppLauncherIntents.add(launchIntent)
}
// Create chooser with created intent list to show email apps of device.
val chooserIntent = Intent.createChooser(Intent(), "OPEN EMAIL APP")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toTypedArray())
startActivity(chooserIntent)
}
Result:
It works on all devices.It will show only Email Apps
public static void shareViaMail(Activity activity, String title, String body, String filePath) {
Uri URI = Uri.parse("file://" + filePath);
final Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"xyz#gmail.com"});
/*if you want to attach something*/
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
try {
activity.startActivity(emailIntent);
} catch (Exception e) {
((BaseActivity) activity).showToast("Gmail App is not installed");
e.printStackTrace();
}
}
Solution is very simple:
Intent testIntent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" + "blah blah body" + "&to=" + "sendme#me.com");
testIntent.setData(data);
startActivity(testIntent);
See: http://www.gaanza.com/blog/email-client-intent-android/
After a lot of searching and testing, I finally found a perfect solution. Thanks to the Open source developer, cketti for sharing his/her concise and neat solution.
String mailto = "mailto:bob#example.org" +
"?cc=" + "alice#example.com" +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Handle case where no email app is available
}
And this is the link to his/her gist.