After looking on th e website, I was not able to find any solution to this problem.
Let me explain a bit more :
I'm trying to send (programmatically) from an App a multipart SMS. It's actually working on my device (Nexus 4) but I tried it on a Galaxy Note, which convert any long-sized SMS to MMS and the app only send short SMS. Any clue about that? Does the Samsung API or something prevent the sendMultipartTextMessage() method from being executed?
In my sending function, I have Log debug, which is shown on screen, so the call to this function is well done..
Thanks for any help!
Here is the code I use :
public static void sendSMS(final Activity a, String phoneNumber, String message)
{
try {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
ArrayList<PendingIntent> sendIntents = new ArrayList<PendingIntent>(parts.size());
for (int i = 0; i < sendIntents.size(); i++)
{
Intent sendInt = new Intent("");
PendingIntent sendResult = PendingIntent.getBroadcast(a.getApplicationContext(), 0, sendInt, PendingIntent.FLAG_ONE_SHOT);
sendIntents.add(sendResult);
}
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
a.getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"), values);
} catch(Exception e) {
e.printStackTrace(System.out);
}
}
public void sendSMS(String phoneNumber, String longMessage) {
try {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(longMessage);
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
add this permission also :
<uses-permission android:name="android.permission.SEND_SMS"/>
above code works for on Samsung Galaxy Note 2 (Android 4.1.1), Micromax HD Canvas (Android 4.1.1) , Samsung Galaxy S3 (Android 4.1.1), HTC One X (Android 4.0).
So this function is not block for me on samsung phones.
Related
My android app's primary purpose is to delete sms messages easily. Since from kitkat, there is a requirement that the app has to be the "default sms app", I ended up creating an app that has all the necessary receivers.
On the SMS Receiver, currently the app is receiving the sms and saving it, with the following code.
public override void OnReceive(Context context, Intent intent)
{
try
{
if (intent.Action != IntentAction) return;
//context.SendOrderedBroadcast(intent, IntentAction);
var bundle = intent.Extras;
if (bundle == null) return;
var pdus = bundle.Get("pdus");
var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var msgs = new SmsMessage[castedPdus.Length];
var sb = new StringBuilder();
String sender = null;
for (var i = 0; i < msgs.Length; i++)
{
var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
msgs[i] = SmsMessage.CreateFromPdu(bytes);
if (sender == null)
sender = msgs[i].OriginatingAddress;
sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress, System.Environment.NewLine, msgs[i].MessageBody));
putSmsToDatabase(context, msgs[i]);
Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show();
}
}
catch (Exception ex)
{
Toast.MakeText(context, ex.Message, ToastLength.Long).Show();
}
}
private void putSmsToDatabase(Context cntxt, SmsMessage sms)
{
ContentValues values = new ContentValues();
values.Put("ADDRESS", sms.OriginatingAddress);
values.Put("DATE", sms.TimestampMillis);
values.Put("READ", 0);
values.Put("STATUS", sms.Status);
values.Put("TYPE", 1);
values.Put("SEEN", 0);
values.Put("BODY", sms.MessageBody);
// Push row into the SMS table
Android.Net.Uri inboxURI = Android.Net.Uri.Parse("content://sms/inbox");
var uri = cntxt.ContentResolver.Insert(inboxURI, values);
}
This code seem to save it, but the problem is that none of the other sms apps shows these messages stored from my app. Everywhere that i searched for SMS receivers, saving part code is not really provided. Just the receiving the sms and the Toast display is provided. This created the below question.
As a default sms app, is it really necessary that my app should save the sms to "content://sms/inbox"?
If yes, What is wrong with the save shown in the above sample. I have a vague understanding that i may have something to do with conversation/threads. But, since i'm getting started with raw android development, probably i have understood it completely wrong. Tried creating the thread using thread_ids, which doesn't seem to work either.
This piece of the puzzle is stopping me from the roll out of the app. Please help.
All column names should be lowercased as it appears that the Sms ContentResolver is constructing "quoted" column strings within the sql statement.
While the statements:
values.Put("ADDRESS", sms.OriginatingAddress);
and
values.Put("address", sms.OriginatingAddress);
Should resolve to a SQL statement that is case insensitive, it is not.
Thus you should provide the columns names as returned from Telephony.TextBasedSmsColumns (which are all lowercase):
Your code becomes:
ContentValues values = new ContentValues();
values.Put("address", sms.OriginatingAddress);
~~~
If your app is targeting a min. of API19:
ContentValues values = new ContentValues();
values.Put(Telephony.TextBasedSmsColumns.Address, sms.OriginatingAddress);
~~~
The SMS ContentProvider was not publicly documented until API 19(?):
Telephony.TextBasedSmsColumns: TextBasedSmsColumns
i am using this code to send sms automatically but is not working ,i am able to send sms by using intent
SmsManager sms = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);
sms.sendTextMessage("+91"+"**********", null, "hii param", sentPI, null);
Toast.makeText(getApplicationContext(), "Your sms sent check your inbox",Toast.LENGTH_LONG).show();
Use Permissions in your Android.manifest file like this
<uses-permission android:name="android.permission.SEND_SMS"/>
And then invoke the SmsManager i.e
SmsManager managerForSms = SmsManager.getDefault();
managerForSms.sendTextMessage("Your text message");
Or you may refer to this Stack's question How to send sms in Android. (See accepted Answer)
Try this code,
public void sendSMS(String phoneNo, String msg){
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
And use this permission in your manifest file
<uses-permission android:name="android.permission.SEND_SMS" />
Update:
Add your country code before pass the phone number
String phoneNo="+91"+editText.getText().toString();
Update 2:
Another possible cause for not working
If u are testing this code in dual sim phone then sim slot 1 always keep active otherwise it " no service" error.
reference - https://stackoverflow.com/a/32090923/3879847
Code Which works for me is this.. Before you need to provide permission in manifest and also need to ask during runtime
<uses-permission android:name="android.permission.SEND_SMS" />
and in Activity
runOnUiThread(new Runnable() {
#Override
public void run() {
String number = SMSEditText.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, Message, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
also if message lenght is greater than 140 or something.. you need to split the message otherwise. it wont work .. which can be acheived by mulitpart text message
I try to send message using SmsManager in android i use this code in my application it work fine only some of the device it is not working.
i just make one class for send message
public class Sms {
public static final String TAG = "Sms";
public void sendSms(String number, String message) {
Log.i(TAG, "Sending " + message + " to " + number);
if (message.length() > 159) {
message = message.substring(0, 159);
}
SmsManager.getDefault().sendTextMessage(number, null, message, null,null);
}
}
this class is for send message
i am send using below code
buddys = db.getAllContacts();
for (int i = 0; i < buddys.size(); i++) {
Sms sms = new Sms();
sms.sendSms(buddys.get(i).getPhoneNumber(), msg_);
Toast.makeText(context.getApplicationContext(), ""+msg_+" to "+buddys.get(i).getPhoneNumber(), Toast.LENGTH_SHORT).show();
}
numbers are come from database there are maximum 4 number. it is working fine in gionee , sony , and micromax , and Moto g But not working in samsung device and zenfone . i put the toast for test it also toast the message in all the devices but the problem is only not send the message. i reused the size of message to 160 i think that was the problem but again same problem is occurs please help me.
From android KitKat when my application is set as the default one, sent sms messages should not be written to the content provider by system:
Note: Beginning with Android 4.4 (API level 19), if and only if an app is not selected as the default SMS app, the system automatically writes messages sent using this method to the SMS Provider (the default SMS app is always responsible for writing its sent messages to the SMS Provider). For information about how to behave as the default SMS app, see Telephony.
But every message I send is automatically written without my interaction (I just call sendTextMessage() and it's taken care of)
May it be a bug in CyanogenMod or I am missing something?
Here is my Code:
#SuppressLint("NewApi")
public static void sendMessage(String messageBody, String phoneNumber,
Context context) {
if (messageBody.length() > 0 && phoneNumber.length() > 0) {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(messageBody);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
if (prefs.getBoolean(SettingsActivity.KEY_SPLIT, false)) {
for (String singlePart : parts) {
sms.sendTextMessage(phoneNumber, null, singlePart, null,
null);
}
} else {
sms.sendMultipartTextMessage(phoneNumber, null, parts, null,
null);
}
}
/**
* Add message to content provider (after KitKat)
*/
// if (android.os.Build.VERSION.SDK_INT >=
// android.os.Build.VERSION_CODES.KITKAT) {
// Log.d(null, "adding sms to content provider");
// ContentValues values = new ContentValues();
// values.put(Telephony.Sms.ADDRESS, phoneNumber);
// values.put(Telephony.Sms.BODY, messageBody);
// // values.put(Telephony.Sms.READ, 0);
// values.put(Telephony.Sms.DATE, System.currentTimeMillis());
// // values.put(Telephony.Sms._ID,
// // msgs[i].getIndexOnIcc());
// context.getContentResolver().insert(Telephony.Sms.Sent.CONTENT_URI,
// values);
// }
}
I don't know why, but when I added sent and delivered pending intent new sms is not automatically added to content provider. This solved my problem
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
sms.sendTextMessage(phoneNo, null, singlePart, sentPI, deliveredPI);
Code from this question: Trouble with sendMultipartText in android
I noticed the same bug in my app on two devices. All with custom roms (CM based).
Try to use this library android smsmms sending library and if the issue persists, it may mean that there is something wrong with your manifest file.
After My android app is able to retrieve a token from a web application, I would like to know how to make it send the token through a SMS to a normal mobile device ( e.g. Iphone, Nokia) ?
Please try following code,
public class SendSMS
{
public static void sendsms(String address,String msgContent)
{
try
{
SmsManager sms = SmsManager.getDefault();
ArrayList<String> smsString = sms.divideMessage(msgContent);
sms.sendMultipartTextMessage(address, null, smsString, null, null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}