SendSMS Question

AllyAndroid

Member
Licensed User
Longtime User
I am using the following code to send a text message. It works without any issues. However, I do get a warning telling me that the variable ps is never assigned a value. If I comment out the line: DIM ps As PhoneSms, the code no longer works.

I was wondering if someone could explain why it works if it is never assigned any value?

B4X:
Sub SendSms(PhoneNumber As String, Text As String)
    Try
        Dim ps As PhoneSms
        Dim r As Reflector
 
        r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
        r.RunMethod4("sendTextMessage", Array As Object(PhoneNumber, Null, Text, Null, Null), _
            Array As String("java.lang.String", "java.lang.String", "java.lang.String", _
                "android.app.PendingIntent", "android.app.PendingIntent"))
        ToastMessageShow("Text was sent to " & PhoneNumber & ".",False)
    Catch
        ToastMessageShow("The text was  not sent to " & PhoneNumber & ".",False)
    End Try   
End Sub
 

NJDude

Expert
Licensed User
Longtime User
That's just a warning, there's no problem, if you don't want to see that warning anymore just add a "'ignore" comment to that line, like this:
B4X:
...
 
Dim ps As PhoneSms 'ignore
 
...
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
This issue started showing up on B4A 2.7x, it is not really a problem, I believe is how the pre-compiler works now, nothing to worry about, I think Erel explained that on a post too.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is related to permissions.

When you call Dim ps As PhoneSms the compiler adds the SEND_SMS permission to the manifest file. You can remove this line and add the permission with the manifest editor:
B4X:
AddPermission(android.permission.SEND_SMS)

My guess is that this code snippet was written before the manifest editor was available.
 
Upvote 0

AllyAndroid

Member
Licensed User
Longtime User
It is related to permissions.

When you call Dim ps As PhoneSms the compiler adds the SEND_SMS permission to the manifest file. You can remove this line and add the permission with the manifest editor:
B4X:
AddPermission(android.permission.SEND_SMS)

My guess is that this code snippet was written before the manifest editor was available.

Thanks Earl, that worked. I had added the following:

B4X:
AddPermission(android.permission.WRITE_SMS)
AddPermission(android.permission.READ_SMS)

but didn't need the SEND_SMS until now.
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I am using the following code to send a text message. It works without any issues. However, I do get a warning telling me that the variable ps is never assigned a value. If I comment out the line: DIM ps As PhoneSms, the code no longer works.

I was wondering if someone could explain why it works if it is never assigned any value?

B4X:
Sub SendSms(PhoneNumber As String, Text As String)
    Try
        Dim ps As PhoneSms
        Dim r As Reflector

        r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
        r.RunMethod4("sendTextMessage", Array As Object(PhoneNumber, Null, Text, Null, Null), _
            Array As String("java.lang.String", "java.lang.String", "java.lang.String", _
                "android.app.PendingIntent", "android.app.PendingIntent"))
        ToastMessageShow("Text was sent to " & PhoneNumber & ".",False)
    Catch
        ToastMessageShow("The text was  not sent to " & PhoneNumber & ".",False)
    End Try  
End Sub


Hello AllyAndroid,
using your code of SendSms, I've ALWAYS a confirmation?
 
Upvote 0
Top