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.
Related
I am using this code to send sms:
This code could be found in many tutorials but it is not working in Oreo, i have sent the correct answer
public void sendSms(String phone) {
if(null != phone) {
final Intent i = new Intent(Intent.ACTION_VIEW);
i.setType("vnd.android-dir/mms-sms");//<-- maybe problem is here
i.putExtra("address", phone);
startActivity(Intent.createChooser(i, getString(R.string.sms)));
}
}
I have tested this code in Android 4 to Android 6 and no problem but in Android 8.1 google api emulator says no app can perform this action, but this emulator already has SMS app installed
Also i do not know if this is working in real devices with Oreo 8.1
If you want the user to go to send SMS without typing phone number in this part
Uri.parse("smsto: " + phone) and let him choose the contact before going to send SMS in Oreo , you can use this code for Oreo and lower versions :
Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"));
sendIntent.putExtra("sms_body", "your message");
if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
sendIntent.setType("vnd.android-dir/mms-sms");
context.startActivity(sendIntent);
finally i found the answer myself:
public void sendSms(String phone, String sms) {
if(null != phone) {
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto: " + phone));
i.putExtra("sms_body", sms);
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
}else{
Toast.makeText(this, "SMS App not found", Toast.LENGTH_LONG).show();
}
}
}
Trying to send a SMS message through my Android App on a device without telephony like a Tablet without user intervention
Api 23
Would like to either:
1) Find a SMS text program that I incorporate into my Android app to send SMS texts using a device that does not have telephony available - I have no idea if this is possible - have tried to write but haven't figured it out.
OR
2) use a SMS app from the Google play store
I have also tried to send through an app (Textme, WhatsApp, Skype) downloaded from Google Play. I don't seem to be able to fill in the TO field see the code below. Lines that have the // have tried
String strPhone = "92xxxxxxxx";
// Uri sms_uri = Uri.parse("smsto:" + strPhone);
Uri sms_uri = Uri.parse("smsto:+92xxxxxxxx");
// Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
Intent i = new Intent(android.content.Intent.ACTION_SEND, sms_uri);
i.setPackage("com.textmeinc.textme");
// i.putExtra("sms_body", "Greetings!");
i.putExtra("sms_body", "Good Morning ! how r U ?");
i.setType("vnd.android-dir/mms-sms");
// i.putExtra(Intent.EXTRA_SUBJECT,"92xxxxxxxx");
i.putExtra("address", "92xxxxxxxx");
i.putExtra(Intent.EXTRA_TEXT, "Hi from me");
// i.putExtra(Intent.EXTRA_PHONE_NUMBER, strPhone);
// i.putExtra(Insert.PHONE, "92xxxxxxxx");
// i.putExtra(ContactsContract.Intents.Insert.PHONE, "(408) 555-1212");
// i.setData(Uri.parse("tel:" + strPhone));
i.setData(Uri.parse("sms:" + strPhone));
i.setType("text/plain");
// i.setType("vnd.android-dir/mms-sms"); does not work
BT_debugger.showIt( "textme" );
i.putExtra("chat",true);
i.putExtra("sms_body", "test");
try {
BT_debugger.showIt( "textme sms found" );
startActivity(i);
} catch (android.content.ActivityNotFoundException ex) {
BT_debugger.showIt( "textme sms not found" );
}
I have implemented the ability to send message in my app and it is working well. But if the user is using another sms app like messenger as their default sms app then, I can't send message to multiple recipients.
If multiple phone number is selected, only one of them get the message in most cases the last phone number.
NOTE: I'm using an implicit intent to send the message and it can send to multiple recipients on stock sms app.
Any help will ve greatly appreciated.
This is what I have as requested
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String defaultSmsPackage = Telephony.Sms.getDefaultSmsPackage(getActivity());
if (defaultSmsPackage != null) {
intent.setPackage(defaultSmsPackage);
}
} else {
Uri numbersUri = Uri.parse("tel:" + phoneNumbers);
intent = new Intent(Intent.ACTION_VIEW, numbersUri);
intent.setType("vnd.android-dir/mms-sms");
}
intent.putExtra("address", phoneNumbers);
intent.putExtra("sms_body", message);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
}
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 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);
}