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?

Code:
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
 
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:
Code:
AddPermission(android.permission.SEND_SMS)

My guess is that this code snippet was written before the manifest editor was available.
 
Upvote 0
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:
Code:
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:

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

but didn't need the SEND_SMS until now.
 
Upvote 0
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?

Code:
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
Back
Top