0

I am trying to send as SMS the data from the database of my Server API. Phone number from the database is used to login the app. Once logged in, a SMS will automatically send to the phone number.

Login class:

                   String phoneNum = et_mobile.getText().toString();
                    int mobile = Integer.parseInt(phoneNum.trim());

                    SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(mobile);
                    String spamMessage = "Srno: " + resObj.getSrNo() +
                            "Date: "  + resObj.getDate() +
                            "In: " + resObj.getTimeIn() +
                            "Out: " + resObj.getTimeOut() +
                            "JO#: " + resObj.getJoNo() +
                            "CoPer: " + resObj.getContactPerson() +
                            "BR: " + resObj.getDesignation() +
                            "CNum: " + resObj.getContactNo();
                    ArrayList<String> sms;
                    sms = smsManager.divideMessage(spamMessage);
                    smsManager.sendMultipartTextMessage(phoneNum, null,sms,null, null);

I already used these permission in my manifest:

    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <uses-permission android:name="android.permission.SEND_SMS"/>

When I login my logcat shows this:

 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.ras, PID: 18982
    java.lang.NumberFormatException: For input string: "09xxxxxxxxx"

and I don't receive any SMS.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Angel
  • 175
  • 1
  • 3
  • 10
  • It is different. It doesn't say that I don't have android permission to send sms. @MikeM. – Angel Dec 02 '19 at 03:06
  • Yes, because we resolved the permissions issue. This is a different issue. This is because you're on a dual-SIM device, and using `SmsManager.getDefault()`. – Mike M. Dec 02 '19 at 03:10
  • When I use `SmsManager.getSmsManagerForSubscriptionId()`, there is an error saying `E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.ras, PID: 18982 java.lang.NumberFormatException: For input string: "09xxxxxxxxx"` @MikeM. – Angel Dec 02 '19 at 03:48
  • @MikeM. I updated my post. Thanks! – Angel Dec 02 '19 at 03:57
  • 1
    Please post the complete stack trace. From the current stack trace posted, it seems you have an invalid input entered into the et_mobile EditText and the exception is thrown from Integer.parseInt() function. – Rahul Shukla Dec 02 '19 at 04:08
  • You should leave phone numbers as strings... Any number that starts with a 0 will be invalid as an integer – OneCricketeer Dec 02 '19 at 04:10
  • 1
    Wait a minute. That `NumberFormatException` is likely coming from your `Integer.parseInt()` call. Firstly, you don't handle phone numbers as integers. Secondly, you don't pass the phone number to `SmsManager.getSmsManagerForSubscriptionId()`. You need to use `SubscriptionManager` to get a list of `SubscriptionInfo`s, and get the subscription ID from those. I linked you to [an example](https://stackoverflow.com/a/51380282). It's at the end of the first code block in that answer. – Mike M. Dec 02 '19 at 04:10
  • It was string before but I changed it because `SmsManager.getSmsManagerForSubscriptionId()` requires an integer – Angel Dec 02 '19 at 04:52
  • In the line of code `SubscriptionManager localSubscriptionManager = SubscriptionManager.from();` what should be inside `SubscriptionManager.from()` ? – Angel Dec 02 '19 at 05:18
  • There is no error. But it is still not sending any sms – Angel Dec 02 '19 at 05:23
  • This is it, right? `SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(0); SmsManager.getSmsManagerForSubscriptionId(simInfo1.getSubscriptionId()).sendTextMessage(phoneNum, null, sms, null, null);` – Angel Dec 02 '19 at 05:29
  • If you're on a dual-SIM device, there should be two `SubscriptionInfo`s in that list. Did you try the other one; i.e., `localList.get(1)`? – Mike M. Dec 02 '19 at 05:33
  • I did. There is still no sms – Angel Dec 02 '19 at 05:35
  • Well, general SMS troubleshooting is a long process; much too long for comments. My last suggestions here are: 1) try sending a short, single-part message with `sendTextMessage()` instead; 2) find the service center number for your SIM, and pass that as the second argument in the `send*Message()` call. The service center number is normally found in your device's main SMS app, in its settings, somewhere. It might be called a Service center, or a Message center, or something else like that. It will be a regular phone number. If you can find it, try passing that in between `phoneNum` and `sms`. – Mike M. Dec 02 '19 at 05:45

2 Answers2

-1

You are getting the NumberFormatException which is described here. As it says

Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

Your string is 09xxxxxxxxx; the xxxxxxxxx cannot be parse into Integer and thus the exception.

You can try out following simple code on a java compiler or an online compiler to see what I am talking about.

public class HelloWorld{

     public static void main(String []args){
        int mobile = Integer.parseInt("09*******");
     }
}
ravi
  • 899
  • 8
  • 31
-1

Try to usemobile variable inside smsManager.sendMultipartTextMessage(**String.valueOf(mobile)**, null,sms,null, null); as using direct string from View can have spaces .

S.Ambika
  • 292
  • 1
  • 14