2

I am developing an Android app, which needs to send a message to multiple recipients.

I used this code to send an SMS to a phone:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("9030500259", null, "sms message", null, null);

I have read many questions in this site which said the above is correct.

On Googling I found this:

http://android-developers.blogspot.in/2013/10/getting-your-sms-apps-ready-for-kitkat.html

According to the article in the above link, some changes happened in messaging.

I am unable to send SMS in my application. What is the correct code to send an SMS in Android version 4.4.x?

smci
  • 32,567
  • 20
  • 113
  • 146
Sanjeev Kumar
  • 17
  • 2
  • 4
  • Are you sure you have the correct permission set in the manifest? – Mike M. Jul 31 '14 at 01:01
  • That piece of code works perfectly fine. As Mike suggested, Add this line to your AndroidManifest.xml file – Srikanth Jul 31 '14 at 01:09
  • yes i am sure ,i added correct permission – Sanjeev Kumar Jul 31 '14 at 01:25
  • i think, in android 4.4.x the above code will not work – Sanjeev Kumar Jul 31 '14 at 01:27
  • In what type of Context are you trying to send the text? That is, are you sending from an Activity? – Mike M. Jul 31 '14 at 01:28
  • That code works just fine in 4.4.x – Mike M. Jul 31 '14 at 01:28
  • yeah i'm sending from activity – Sanjeev Kumar Jul 31 '14 at 01:30
  • Then it sounds like there are other problems. Are you sure the code is being reached? – Mike M. Jul 31 '14 at 01:32
  • @MikeM. Have you seen the link i specified – Sanjeev Kumar Jul 31 '14 at 01:33
  • Many, many times. I consulted it, for example, when I was writing text messaging into my own app on 4.4.2. – Mike M. Jul 31 '14 at 01:35
  • @Mike M. If I remove send SMS permission from manifest file,runtime error is occurring,if I add it no error occurs but message not going – Sanjeev Kumar Jul 31 '14 at 02:45
  • How are you determining that they are "not going"? – Mike M. Jul 31 '14 at 02:46
  • @MikeM. i checked the receipents phone,no message sent from the app to receipent – Sanjeev Kumar Jul 31 '14 at 04:46
  • Have you added a log print after the `sendTextMessage()` call to make sure it's being reached? If you're sure that it is, you might setup PendingIntents and a BroadcastReceiver to check if there are any problems. This answer should give you an idea how to: http://stackoverflow.com/questions/24673595/how-to-get-sms-sent-confirmation-for-each-contact-person-in-android/24845193#24845193 – Mike M. Jul 31 '14 at 04:54
  • I just had another thought: make sure you're importing the correct SmsManager. Should be `android.telephony.SmsManager`. _Not_ `android.telephony.gsm.SmsManager`. – Mike M. Jul 31 '14 at 05:09
  • @MikeM. i am sure i am importing correct library,i did one thing,i kept the above code at onCreate() of activity,sms is working properly, – Sanjeev Kumar Jul 31 '14 at 08:02
  • when i keep the above code in for loop ,to send message to different receipents,sms is not working, – Sanjeev Kumar Jul 31 '14 at 08:03
  • Well, the link I gave in a previous comment is an example of how to do exactly that. Tested and verified on 4.4.2. – Mike M. Jul 31 '14 at 08:10
  • i think the real problem is ,in my phone android 4.4.4 is available,when a sms is about to send by the third party app,it shows a dialog saying " this app is about to send message " with two buttons allow and dont allow ,so if 10 messages are to be sent ,for every message it should show dialog,as i'm writing in for loop ,i think when the first time dialog appears,it conflicts with second sendtextmessage() – Sanjeev Kumar Jul 31 '14 at 08:20
  • 1
    There is a per-hour limit built into Android, and some providers restrict it even further. If you've already hit that limit while testing, then that's what that is. – Mike M. Jul 31 '14 at 08:27
  • 1
    @MikeM. you are awesome,i tried the code in the link you specified,its working – Sanjeev Kumar Jul 31 '14 at 08:28
  • @MikeM. i now discovered that ,the problem why messages are not sent is message itself,i changed the message to "hello hi " it gets sent – Sanjeev Kumar Aug 01 '14 at 15:29
  • is there any specifications for message,like message length,valid characters etc., – Sanjeev Kumar Aug 01 '14 at 15:31
  • @MikeM. i figured out the solution finally,actually we can send messages of length not morethan 160 chracters with sendTextMessage(), – Sanjeev Kumar Aug 01 '14 at 17:16
  • On dual sim phones if no sim is set as default for sending sms, it throws "RESULT_ERROR_GENERIC_FAILURE" error. Therefore a sim must be set as active. You can find more information in this link! http://stackoverflow.com/questions/27351936/how-to-send-a-sms-using-smsmanager-in-dual-sim-mobile – Abdul kadir May 06 '17 at 06:08

1 Answers1

0

i was facing same problem .but finally got solution . if u are testing this code in dual sim phone then sim slot 1 always keep active otherwise it " no service" error inn pendingsentintent.

Kamal Bunkar
  • 1,354
  • 1
  • 16
  • 20